I am trying to create a component that has a dynamic template string inside it that can access local variables in the template. Each approach I tried ends up with the fact that the "dynamic line of the template" is not $compile 'd (angular 1 terminology, please excuse me).
Here is the code for the component below. Where you see the comment, I would like to insert a template line that can refer to item in ngFor .
@Component({ selector: 'ion-alpha-scroll', template: ` <ion-scroll [ngStyle]="calculateScrollHeight()" scrollX="false" scrollY="true"> <ion-list class="ion-alpha-list-outer"> <div *ngFor="let items of sortedItems | mapToIterable;"> <ion-item-divider id="scroll-letter-{{items.key}}">{{items.key}}</ion-item-divider> <ion-item *ngFor="let item of items.value"> </ion-item> </div> </ion-list> </ion-scroll> <ul class="ion-alpha-sidebar" [ngStyle]="calculateDimensionsForSidebar()"> <li (click)="alphaScrollGoToList(letter)" *ngFor="let letter of alphabet"> <div class="letter">{{letter}}</div> </li> </ul> `, pipes: [MapToIterable] }) export class IonAlphaScroll { @Input() listData: any; @Input() key: string; @Input() template: string; .... }
Ideally, I would like to have the translated content of the ion-alpha-scroll item ngFor in ngFor . I tried using ng-content in the required ngFor component and no luck -
<ion-alpha-scroll *ngIf="breeds" [listData]="breeds" key="$t"> {{item.$t}} </ion-alpha-scroll>
One thing I tried was this:
<ion-alpha-scroll *ngIf="breeds" [listData]="breeds" key="$t" [template]="alphaScrollTemplate"> </ion-alpha-scroll>
alphaScrollTemplate is just a string containing {{item.$t}} . Then I tried to reference it in the component where the comment asks the question, but it does not work -
... <ion-item *ngFor="let item of items.value"> {{template}} </ion-item> ...
I am really wondering if this is possible even with angular 2. I just found this question, which is very similar to mine . Any help or suggestions would be greatly appreciated, thanks.
angular angular2-components
Ross
source share