Apply attribute directive to the ngForTemplate of each item

Discussão em 'Angular' iniciado por Mike, Setembro 25, 2024.

  1. Mike

    Mike Guest

    I am building a standalone component that receives a collection of objects to render with ngFor. The templateRef, use to render those objects is taken by means of a ContentChild.

    <ng-template
    ngFor
    let-item
    [ngForOf]="items"
    [ngForTemplate]="template"
    [ngForTrackBy]="trackByFunction"
    >
    </ng-template>


    Here is the usage:

    <my-component [items]="items">
    <ng-template let-item>
    {{item.name}}
    </ng-template>
    </my-component>


    I would like to add a directive to each item that would do some simple attribute stuff, like add a class, hold a dimension. The idea is that this component will be used instead ngFor to do more advanced logic. I want to avoid adding the directive to the html on the usage side. I also want to be able to reach this directive from MyComponent.

    How can I do this?

    Edit: Even if I'd place the directive on the usage side, I still need a way to query for that rendered items somehow. So far I found no combination of ContentChild/ViewChild and read that would help here.

    Continue reading...

Compartilhe esta Página