假设我有一个名为“ my-custom-directive”的指令和一个名为“ app.component”的组件我在app.component中有一个方法-createDirective(),当单击来自app-component.html的按钮时将调用该方法我想从此方法实例化我的自定义指令。如何在angular2及更高版本中完成此操作?
我尝试了Renderer2,但无法从组件创建指令
import { Directive, HostListener } from '@angular/core';
@Directive({
selector: '[my-custom-directive]'
})
export class MyCustomDirective {
constructor() { }
@HostListener('click', ['$event']) onClick($event: Event) {
// Do what ever you want to do ....
}
}
// In Your Component HTML :
<button my-custom-directive></button>
// Don't forget to include the references in your Module.
// See if this Solves your purpose.