Using ng-content inside a for loop

2,034 views
Skip to first unread message

Christophe HOARAU

unread,
Oct 5, 2016, 7:42:23 AM10/5/16
to AngularJS
Hi,

I would like to know if its possible to define a template(transclusion), then call it inside a for loop in the directive template.
I have tried but it does nothing, no error and no content is generated in my loop.

Here is a sample of the code :

<myComponent>
 
<div myTemplateContent>test</div>
</myComponent>


then in "myComponent" code I have :
<div *ngFor="let a of myList">
{{a}}<ng-content select="[
myTemplateContent]"></ng-content>
</div>

in this case ng-content renders nothing at all.

Thanks

Marco Ramos

unread,
Oct 6, 2016, 8:22:24 AM10/6/16
to AngularJS
Check out https://toddmotto.com/transclusion-in-angular-2-with-ng-content.

I think you want one of the following: (check out https://toddmotto.com/transclusion-in-angular-2-with-ng-content)


<myComponent>
  
<div class="myTemplateContent">test</div>
</myComponent>


<div *ngFor="let a of myList">
{{a}}<ng-content select=".myTemplateContent"></ng-content>
</div>

OR

<myComponent>
  
<myTemplateContent>test</
myTemplateContent>
</myComponent>

Christophe HOARAU

unread,
Oct 6, 2016, 1:13:23 PM10/6/16
to AngularJS
Hi,

Thanks but this does not work inside of ngFor, it works great as long as I don't put ng-content inside of the ngFor loop.
Reply all
Reply to author
Forward
0 new messages