[英]how can i have angular2 update content after dynamically loading a component
我有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"
如果您在artists
没有null
元素,则不需要?
在{{a?.name}}
(在*ngFor
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.