Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What IS ng-component in Angular?

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?

like image 465
Simon_Weaver Avatar asked Aug 29 '19 04:08

Simon_Weaver


2 Answers

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: '' })
like image 77
Simon_Weaver Avatar answered Sep 17 '22 15:09

Simon_Weaver


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.

like image 41
JoshG Avatar answered Sep 21 '22 15:09

JoshG