简体   繁体   中英

List item renderer in Angular 2

I got "dumb" list component that uses another "dumb" component to render single item. And when I want to pass data to renderers I got to have same property in my list component. For example if I want to have "showTimestamp" boolean property for item I got to do this:

list.template.html

<my-item-renderer *ngFor="let item of items" [showTimestamp]="showTimestamp"></my-item-renderer>

Is there a way to do something to replace part of component template?

I would like to do something like this:

<my-list [items]="items"><ng-content><item-renderer [showTimestamp]="true"></item-renderer></ng-content></my-list>

So my list get just items property and in ng-content gets a renderer with already set showTimestamp variable.

You can use template for this:

<my-list [items]="items">
  <template><item-renderer [showTimestamp]="true"></item-renderer></template></my-list>
@Component({
  selector: 'my-list',
  template: `
      <template ngFor [ngForOf]="items" [ngForTemplate]="templateRef"></template>`
})
class MyList {
  @Input() data;
  constructor(private templateRef:TemplateRef) {}
}

See also

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM