There are two very similar named directives (or attributes) in angular
ng-component
and ng-container
If you put ng-component
instead of ng-container
you'll get all kinds of weird behavior - such as automatic insertion of a <router-outlet>
.
I've often wondered is there actually a purpose for ng-component
in user code.
Is it a legacy thing? Is it internal only? Does it solve any problems?
Not sure why this would ever be beneficial, but turns out if you don't specify a selector name then your component will be rendered within ng-component
tags in the HTML.
@Component({ selector: '' })
Angular routers render the components they have navigated to using a router outlet directive. Unless a specific router outlet directive is specified, Angular will automatically place the routed component within an <ng-component>
element (i.e., a default router outlet directive). You generally don't use this directive directly. You'd invoke it by using a <router-outlet>
element. In other words, ng-component
is the default tag name of the injected element, if another directive isn't given.
So yes, it is generally used internally. As stated here:
You could attach styles to the "ng-component" selector; however, given the fact that a parent component may contain multiple router-outlet elements (for named and unnamed views), providing a unique local handle would make CSS selectors a bit more intuitive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With