I am using Angular 2-rc3 and have Component , and I want to apply the transition, just differently. Here is my component:
import { Component, Input } from '@angular/core'; @Component({ selector: 'my-list', template: `<ul> <li *ngFor="let item of data"> -- insert template here -- <ng-content></ng-content> </li> </ul>` }) export class MyListComponent { @Input() data: any[]; }
And I use it as follows:
<my-list [data]="cars"> <div>{{item.make | uppercase}}</div> </my-list>
As you can see, I am trying to determine the inline template that will be used by my component. Now this is going terribly wrong. First, a data binding exception saying can't read property 'make' of undefined . It is trying to read item.make off of my surrounding component, not MyListComponent . But even if I temporarily disable it now:
<my-list [data]="cars"> <div>{item.make | uppercase}</div> </my-list>
Then the second problem appears:
-- insert template here -- -- insert template here -- -- insert template here -- -- insert template here -- {item.make | uppercase}
So, Angular does not actually copy the template for use in *ngFor , it just binds the elements and is ultimately bound to the last element.
How do I make this work?
I had the same issue with AngularJS where petebacondarwin posted a solution for managing the DOM through compilation , which was great. I have this option with Angular 2 by injecting ElementRef into my component, but! One big difference is that the compile in AngularJS was deleted before the data binding, which means that the template had no problems using {{item.make}} . With Angular 2, this seems bad, as {{item}} parsed in advance. So what is the best way to do this? Using a slightly different notation [[item]] and a line replacing the whole thing does not seem the most elegant way ...
Thanks in advance!
// Edit: Here is Plnkr , which reproduces the problem.
angular angular2-components
JP ten berge
source share