Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we have two ng-content with same select attribute?

Tags:

angular

<div class="main">
  <ng-content select="[body]"></ng-content>
</div>
<div class="main-copy">
  <ng-content select="[body]"></ng-content>
</div>

I am trying to copy the same content into main-copy but unfortunately it is not working.

Any suggestion?

like image 895
Ashutosh Singh Avatar asked Oct 18 '16 11:10

Ashutosh Singh


People also ask

Can we use multiple Ng-content?

To add multiple ng-content, we need to add the select attribute as shown below. The select attribute property creates a mapping between the templates where these need to be transcluded.

What is the limitation of multiple slot content projection?

Known limitations of our implementation are as follows: You cannot data-bind the slot's name attribute. You cannot data-bind the slot attribute. You cannot dynamically generate slot elements inside a component's view.

What is Contentprojection in Angular?

Content projection is a pattern in which you insert, or project, the content you want to use inside another component. For example, you could have a Card component that accepts content provided by another component.

What is the difference between Ng-content ng-container and ng-template?

To sum up, ng-content is used to display children in a template, ng-container is used as a non-rendered container to avoid having to add a span or a div, and ng-template allows you to group some content that is not rendered directly but can be used in other places of your template or you code.


2 Answers

update Angular 5

ngOutletContext was renamed to ngTemplateOutletContext

See also https://github.com/angular/angular/blob/master/CHANGELOG.md#500-beta5-2017-08-29

original

No, that's not supported.

Every element matching the selector will be projected to the first <ng-content>.

Perhaps ngTemplateOutlet or ngForTemplate is what you're looking for.

See also

  • How to repeat a piece of HTML multiple times without ngFor and without another @Component
like image 72
Günter Zöchbauer Avatar answered Sep 28 '22 06:09

Günter Zöchbauer


For future references, here is a working example of multiple transclusion which utilizes ngTemplateOutlet.

Reference

  • reusable components ngTemplateOutlet
like image 24
realappie Avatar answered Sep 28 '22 08:09

realappie