我正在开发一个将dropdownlist转换为radioListbox的angular指令。这是我的初始代码:
import { Directive, Input, TemplateRef, ViewContainerRef,OnInit } from '@angular/core';
@Directive({
selector: '[radioList]'
})
export class RadioListDirective implements OnInit {
constructor(private templateRef: TemplateRef<any>, private vcRef: ViewContainerRef) {
}
ngOnInit() {
console.log(this.templateRef);
this.vcRef.createEmbeddedView(this.templateRef);
}
}
和
<div>
test
</div>
<select *radioList><option>1</option><option>2</option></select>
它应该记录其TemplateRef
的nativeElement是ElementRef
的select
。但结果是空的评论,其下一个元素是select
。
Hacky解决方案目前有效:
this.templateRef.elementRef.nativeElement.nextSibling
(this.viewContainerRef.get(0) as any).rootNodes[0]
(注意,从您的示例代码中,您使用vcRef
而不是viewContainerRef
,就像我在这里使用的那样。)