通過結構指令,我將如何獲得指令所在的(本地)元素的持有? 與正常的指令後,ElementRef有它的nativeElement指向它 - 比如:結構指令 - 找到它放置的元素
<input type="text" example>
@Directive({
selector: '[example]'
})
export class ExampleDirective {
constructor(private el: ElementRef) {}
ngAfterViewInit() {
console.log(this.el.nativeElement); // the input
}
}
但隨着結構性指令,它指向模板評論 - 如。
<input type="text" *example>
@Directive({
selector: '[example]'
})
export class ExampleDirective {
constructor(
private el: ElementRef,
private view: ViewContainerRef,
private template: TemplateRef<any>
) {}
ngAfterViewInit() {
this.view.createEmbeddedView(this.template);
console.log(this.el.nativeElement); // template bindings comment
// how to find the input itself?
}
}
我使用ViewContainerRef,TemplateRef,@ContentChild,@ViewChild的各種組合嘗試 - 只是似乎沒有能夠找到輸入元素本身......
你試圖解決的實際問題是什麼? –