Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering <ng-content> in angular 2 more times

I have code like this

<template *ngIf='mobile'>
  <div class='wrapper'>
    <div class='nav'>
        <ng-content></ng-content>
    </div>
  </div>
</template>

<template *ngIf='desktop'>
  <ng-content></ng-content>
</template>

However, Angular 2 renders ng-content just one time. Is there a way to get this case working properly without too much hacking?

like image 751
Haris Bašić Avatar asked Feb 12 '17 15:02

Haris Bašić


People also ask

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 Ng-content in Angular stack overflow?

ng-content is used to project content into Angular components. You use the <ng-content></ng-content> tag as a placeholder for that dynamic content, then when the template is parsed Angular will replace that placeholder tag with your content. <app-child></app-child> would not be visible.

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 Ngcontent in Angular?

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.


1 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

You can pass the content as a template, then you can render it multiple times.

<parent>
 <template>
  projected content here
 </template>
</parent>

In parent

<ng-container *ngIf='mobile'>
  <div class='wrapper'>
    <div class='nav'>
        <template [ngTemplateOutlet]="templateRef"></template>
    </div>
  </div>
</ng-container>

<template *ngIf='desktop'>
  <template [ngTemplateOutlet]="templateRef"></template>
</template>

export class ParentComponent {
  constructor(private templateRef:TemplateRef){}
}

You can also pass data to the template to bind with the projected content.

<ng-container *ngIf='mobile'>
  <div class='wrapper'>
    <div class='nav'>
        <template [ngTemplateOutlet]="templateRef" [ntOutletContext]="{ $implicit: data}"></template>
    </div>
  </div>
</ng-container>

<ng-container *ngIf='desktop'>
  <template [ngTemplateOutlet]="templateRef" [ntOutletContext]="{ $implicit: data}"></template>
</ng-container>

export class ParentComponent {
  @Input() data;

  constructor(private templateRef:TemplateRef){}
}

and then use it like

<parent [data]="data">
 <template let-item>
  projected content here {{item}}
 </template>
</parent>

See also My own Angular 2 Table component - 2 way data binding

like image 132
Günter Zöchbauer Avatar answered Sep 30 '22 13:09

Günter Zöchbauer