Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to select by id in Angulars <ng-content>?

I'm playing around with Angular's (version 7) content projection. As far as I know, it's possible to select by attribute, class, and tag with the select attribute of <ng-content>.

I've also tried to select by id:

<ng-content select="#myID"></ng-content>

From:

<mycomponent>
   <div id="myid">
        Test
   </div>
</mycomponent>

But it doesn't seem to work.

Why does the selection of IDs not work?

like image 225
Robin Avatar asked Nov 13 '18 16:11

Robin


People also ask

What is Ng-content select?

The ng-content is used when we want to insert the content dynamically inside the component that helps to increase component reusability. Using ng-content we can pass content inside the component selector and when angular parses that content that appears at the place of ng-content.

Can we style ng-content?

ng-content is an important concept to create reusable and flexible components. With the help of ng-content content can be projected by a parent component into a predefined slot. In some cases, you want to apply some styles to the projected content.

What is Ng-content tag in Angular?

The <ng-content> element specifies where to project content inside a component template.


1 Answers

I can't give a reason why you can't target an ID using the syntax like #myID, but you are able to target attributes on DOM nodes using a syntax like [attribute=value] (like you are able to in CSS selectors). Using this, you can then target for a specific ID attribute. In your case you would want to use the following:

<ng-content select="[id=myID]"></ng-content>
like image 82
Daniel W Strimpel Avatar answered Oct 19 '22 06:10

Daniel W Strimpel