Lea*_*ode 6 angular2-ngcontent angular
我成功地<ng-content></ng-content>用来在孙子中显示一个组件,如下所示:带有客户组件的父对子组件树,但现在我想@ContentChild在那个孙子中使用来访问传入的内容,但我似乎无法得到任何工作.
这是我尝试使用选择器和@ContentChildstackBlitz:https://stackblitz.com/edit/angular-dpu4pm
.无论我做什么,@ContentChild总是在TheChild组件中未定义,但在TheParent组件中工作.
@Component({
selector: 'spinner',
template: `
spinner
`,
})
@Component({
selector: 'my-app',
template: `
works <the-parent><spinner #spin></spinner></the-parent>
`,
})
@Component({
selector: 'the-parent',
template: 'this is parent <the-child><ng-content #content></ng-content></the-child>'
})
export class TheParent implements AfterViewInit{
@ContentChild('spin') spin;
ngAfterViewInit() {
alert('parents spin is '+typeof this.spin); //object
}
}
@Component({
selector: 'the-child',
template: 'this is child <ng-content></ng-content>'
})
export class TheChild implements AfterViewInit{
@ContentChild('spin') spin;
@ContentChild('content') content;
ngAfterViewInit() {
alert('childs spin is '+typeof this.spin); //undefined
alert('childs content is '+typeof this.content); //undefined
}
}
Run Code Online (Sandbox Code Playgroud)
关于如何进行这项工作的任何建议,或任何其他方式从孙子访问祖父母将非常感激.
| 归档时间: |
|
| 查看次数: |
2607 次 |
| 最近记录: |