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

Ash*_*oyi 10 angular2-template angular

我正在使用一个库,希望我指定一个指令的主体作为template元素的子元素

<template customDirective>
   <custom-element #lookup></custom-element>
</template>
Run Code Online (Sandbox Code Playgroud)

有没有办法访问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
  }
}
Run Code Online (Sandbox Code Playgroud)

我遇到undefined了两种情况.

yur*_*zui 9

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

尝试使用setter,如:

@ViewChild('lookup') 
set lookUp(ref: any) {
  console.log(ref);
}
Run Code Online (Sandbox Code Playgroud)

Plunker示例