有谁知道如何遍历Angular 2 ng-content项吗?
我需要检查在ng-content中存在的项上定义的属性的值是什么。有谁知道怎么弄到它们吗?
最好的方案是在其中获取实际的组件实例,因为我可以在组件中设置一些内部属性,直接访问它们会很棒!
一些示例代码:
// ====================== Container Component =======================
import * as ng from "angular2/angular2";
@ng.Component({
selector: 'grid-layout',
lifecycle: [ng.onInit]
})
@ng.View({
templateUrl: `<div><ng-content></ng-content></div>`
})
export class GridLayout
{
constructor(){
}
private onInit(): void {
// ??? something like this.ngContent would be great :)
}
}
// ====================== Caller Component =======================
import * as ng from "angular2/angular2";
@ng.Component({
selector: 'my-page'
})
@ng.View({
templateUrl: `
<grid-layout>
<div data-att1="container 1">1</div>
<div data-att2="container 2">2</div>
<div>3</div>
</grid-layout>`,
directives:[GridLayout]
})
export class MyPage
{
constructor(){
}
}发布于 2016-03-24 03:43:58
只需使用@ContentChildren注入轻量级DOM的内容
ParentComponent implements AfterContentInit{
@ContentChildren(ChildComponent) contents : QueryList<ChildComponent>;
ngAfterContentInit()
{
//access contents here
}
}https://stackoverflow.com/questions/31708366
复制相似问题