Chrillewoodz
Chrillewoodz

Reputation: 28328

Angular 2 query for transcluded content in a specific ng-content

I would like to query my template for transcluded content by accessing one of the ng-content tags, preferably by its select attribute, without setting a local variable on the element.

Is this possible?

<comp>
  <span world>Hello world</span>
</comp>

Template:

<div>
  <ng-content select="[world]"></ng-content>
</div>

Comp.ts:

@ContentChild('world') world: ElementRef;

The above does not work, which was expected since I was merely shooting from the hip.

How can I query the ng-content for its content?

The use case is to check whether content has actually been passed to the component.

Upvotes: 1

Views: 905

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657268

For ng-content to select your span you would need to add a world attribute

<comp>
  <span world>Hello world</span>
</comp>

You can't use @ContentChild() to query for elements without a template variable or a component or directive type.

Perhaps this approach helps Angular2 Dart - Get Text inside Angular2 Component

Upvotes: 2

Related Questions