参见英文答案 >
How to instantiate and apply directives programmatically? 1个
上下文
上下文
我有一个简单的指令,根据接收的属性为给定的HTML元素添加一些属性.
<button class="btn btn-blue x-large" [myDirective]="{ some_json_data: true }"> Unfold </button>
myDirective指令只是在ngOnInit钩子中执行一些逻辑并装饰ElementRef本机元素(在本例中为按钮)添加属性,没有什么复杂的.
ngOnInit(): void { const el: Element = this.element.nativeElement; this.decorate(el,this.myDirective); }
问题
基于给定的逻辑(在myDirective修饰中)我想在myDirective中向ElementRef引用的元素添加工具提示(这是另一个指令).
如何手动挂载指令以及如何将其添加到元素(ViewContainerRef)?