angular – 访问模板元素内的模板引用

前端之家收集整理的这篇文章主要介绍了angular – 访问模板元素内的模板引用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用一个库,希望我指定一个指令的主体作为模板元素的子元素
  1. <template customDirective>
  2. <custom-element #lookup></custom-element>
  3. </template>

有没有办法在我的组件中访问自定义元素#lookup.

例如,

  1. @Component({
  2. selector: 'app-test',template: `
  3. <template customDirective>
  4. <custom-element #lookup></custom-element>
  5. </template>
  6. `
  7. })
  8. export class TestComponent {
  9. @ViewChild('lookup') viewChildRef;
  10. @ContentChild('lookup') contentChildRef;
  11.  
  12. constructor() {
  13. }
  14.  
  15. ngAfterContentInit(): void {
  16. console.log(this.viewChildRef); // <-- undefined
  17. console.log(this.contentChildRef); // <-- undefined
  18. }
  19. }

在这两种情况下我都没有定义.

在不创建嵌入视图之前,无法获取模板内部组件的引用.

尝试使用setter,如:

  1. @ViewChild('lookup')
  2. set lookUp(ref: any) {
  3. console.log(ref);
  4. }

Plunker Example

猜你在找的Angularjs相关文章