Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying content of Component that i fetched with @ContentChildren or @ViewChild

So I'm trying to display child components at specific locations in my Component.

I have the following:

@Component({
  selector: 'app-item-list',
  template: `
<p *ngFor="let item of items">
  {{item.order}}
  <!-- display content of child component here -->
</p>`
})
export class ItemListComponent implements OnInit, AfterContentInit {

  //Get all child components of the type ItemComponent and store them.
  @ContentChildren(ItemComponent, {descendants: true}) 
  items?: QueryList<ItemComponent>;
  constructor() { }

  ngOnInit() {
  }
    
  ngAfterContentInit(): void {
      
  }

}

@Component({
  selector: 'app-item',
  template: `
<p>
  item works!
</p>
<ng-content></ng-content>
`
})
export class ItemComponent implements OnInit {

  @Input() order?: string;
  constructor() { }

  ngOnInit() {
  }

}

I tried to display it using <ng-content select="item"> but that didn't work.

With the following:

<app-item-list>
  <app-item order="2">
    test2
  </app-item>
  <app-item order="1">
    test1
  </app-item>
</app-item-list>

the expected output is

2
item works!
test2
1
item works!
test1

https://stackblitz.com/edit/angular-ivy-2aibgt

like image 518
Peter Avatar asked Jul 16 '20 17:07

Peter


People also ask

What is the difference between @ViewChild and @ContentChild?

So, the answer to your question is pretty simple: The difference between @ViewChildren and @ContentChildren is that @ViewChildren look for elements in Shadow DOM while @ContentChildren look for them in Light DOM.

What is the difference between ViewChild and ViewChildren?

The ViewChild or ViewChildren decorators are used to Query and get the reference of the DOM element in the Component. ViewChild returns the first matching element and ViewChildren returns all the matching elements as a QueryList of items. We can use these references to manipulate element properties in the component.

What is ContentChild and ContentChildren in Angular?

The ContentChild & ContentChildren are decorators, which we use to Query and get the reference to the Projected Content in the DOM. Projected content is the content that this component receives from a parent component. The ContentChild & ContentChildren is very similar to the ViewChild & ViewChildren.

What do you understand by ViewChild and ContentChild?

ViewChild is used to select an element from component's template while ContentChild is used to select projected content.


1 Answers

I can suggest you a solution that is used in Angular material library.

  • Wrap content of app-item component in <ng-template> tag

item.component.html

<ng-template>  <----------------------------- wrapper
  <p>
    item works!
  </p>
  <ng-content></ng-content>
</ng-template>
  • then we can grab a reference to that TemplateRef

item.component.ts

@ViewChild(TemplateRef, {static: true}) template: TemplateRef<any>;
  • finally we can use it in a loop of app-item-list component

item-list.component.html

<p *ngFor="let item of items">
  {{item.order}}
  <ng-template [ngTemplateOutlet]="item.template"></ng-template>
</p>

Forked Stackblitz

like image 96
yurzui Avatar answered Oct 19 '22 13:10

yurzui