我有angular2最终版本。已经创建了一个组件
@Component({
selector:'artists',
template: `
<h2>Artists Information</h2>
<ul [style.display]="artists?.length>0 ? 'block':'none'">
<li ngfor="a in artists">Name: {{a?.name}} - Followers: {{a?.folloers.total}}</li>
</ul>
`
})
export class Artists{
@Input("list")
private artists:any;
}
稍后在程序的某个地方我尝试动态加载组件
@ViewChild('divBody', {read: ViewContainerRef}) private body: ViewContainerRef;
this.body.clear();
let factory = this.resolver.resolveComponentFactory(Artists);
let injector = ReflectiveInjector.fromResolvedProviders([], this.body.parentInjector);
let b= this.body.createComponent(factory,0,injector,[]);
// data is comming from an ajax call to a remote location.
b.instance.artists = data.artists.items;
b.changeDetectorRef.detectChanges();
我的问题是艺术家视图未更新并显示空白列表行而不是艺术家列表。
问题是我错过了什么?为什么即使在我调用detectChanges()之后,模板也不用数据更新?
<li ngfor="a in artists"
应该
<li *ngFor="let a of artists"
如果你没有null
元素 inartists
你不需要?
in {{a?.name}}
(within *ngFor
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句