【发布时间】:2017-07-20 22:41:20
【问题描述】:
我正在使用一个库,它希望我将指令的主体指定为 template 元素的子元素
<template customDirective>
<custom-element #lookup></custom-element>
</template>
有没有办法在我的组件中访问custom-element#lookup。
例如,
@Component({
selector: 'app-test',
template: `
<template customDirective>
<custom-element #lookup></custom-element>
</template>
`
})
export class TestComponent {
@ViewChild('lookup') viewChildRef;
@ContentChild('lookup') contentChildRef;
constructor() {
}
ngAfterContentInit(): void {
console.log(this.viewChildRef); // <-- undefined
console.log(this.contentChildRef); // <-- undefined
}
}
在这两种情况下我都会收到undefined。
【问题讨论】: