我DynamicComponentLoader
用来加载子组件,它产生以下html:
<child-component> Child content here </child-component>
这是我如何在父级中加载组件
ngAfterViewInit(){
this._loader.loadIntoLocation(ChildComponent,this._elementRef,'child');
}
如何将id属性添加到子组件,以便产生以下html:
<child-component id="someId" > Child content here </child-component>
如果可能的话,我将添加一个字段ChildComponent
并绑定id
到它:
@Component({
selector: 'child-component',
host: {'[id]': 'id'}
})
class ChildComonent {
id:string;
}
this._loader.loadIntoLocation(ChildComponent,this._elementRef,'child')
.then(cmp => cmp.instance.id = 'someId');
另请参见http://plnkr.co/edit/ihb7dzRlz1DO5piUvoXw?p=preview#
一种更hacky的方式是
this.dcl.loadIntoLocation(Dynamic, this.ref, 'child').then((cmp) => {
cmp.location.nativeElement.id = 'someId';
});
代替nativeElement
直接访问属性,应该可以使用进行相同的操作Renderer
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句