2

When using transclusion in an angular2 component, it appears that my 'ViewChildren' is not able to detect these components. When I make a component where I define the same components directly then it works fine. Does anyone now a way to get this work?

Joris Brauns
  • 263
  • 1
  • 3
  • 17

2 Answers2

5

You can query transcluded content using @ContentChild() or @ContentChildren().

@ViewChild() and ´@ViewChildren()` only allow to query element in the components template.

See also angular 2 / typescript : get hold of an element in the template

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
0

I tried to do this with my error messages component too. Actually you just need to use ContentChildren decorator instead of ViewChildren. It is made special for ng-content elements. As a result you will get QueryList which is iterable object of your components or elements.

Lyubimov Roman
  • 1,269
  • 15
  • 27