question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ViewContainerRef.createComponent inside a structural directive adds the component as a SIBLING of the element the directive is called on

See original GitHub issue

Description

I have a requirement of creating a structural directive that creates a child component inside inside the element where the directive is being called. If I use ViewConainerRef.createComponent component is injected as a SIBLING of the element where the directive is called. i.e:

If I write a directive called *fooDirective, Like this and want to inject <foo-component>:

<div *fooDirective></div>

Here is what it currently outputs:

<div></div>
<foo-component></foo-component>

And what I was expecting was this:

// what I was expecting
<div>
    <foo-component></foo-component>
</div>

I know I’ve found some issues that indicate ViewContainerRef.createComponent injects new components as siblings by design, but I want to know for my specific instance it works this way by design as well. If it does is there a workaround we can do to get by?

Some clarification would go a very long way as I feel it kind-of is not well documented explicitly.

Thanks.

🔬 Minimal Reproduction

https://stackblitz.com/edit/angular-insert-component-in-structural-directive?file=app%2Fapp.component.ts

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

0reactions
angular-automatic-lock-bot[bot]commented, Sep 15, 2019

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ViewContainerRef - Angular
Anchor element that specifies the location of this container in the containing view. Each view container can have only one anchor element, and...
Read more >
Angular2: Insert a dynamic component as child of a container ...
Using the SomeComponent 's ViewContainerRef has the same result, it is still inserting the generated component as a sibling, not a child. I ......
Read more >
[angular] All about the ViewContainerRef - Medium
ViewContainerRef (VCR) represents a container where one or more view can be attached. ... and that's how we can create structural directive.
Read more >
How to Write a Custom Structural Directive in Angular - Part 2
add constructor with TemplateRef , and ViewContainerRef injected · add an @Input property to hold the array of items that we want to...
Read more >
Angular Dynamic Components: Building a Dialog System
In this tutorial, you will learn what dynamic components are and how they ... All we have to do with that directive is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found