实例化点击时角度组件的角度自定义指令

问题描述 投票:0回答:1

假设我有一个名为“ my-custom-directive”的指令和一个名为“ app.component”的组件我在app.component中有一个方法-createDirective(),当单击来自app-component.html的按钮时将调用该方法我想从此方法实例化我的自定义指令。如何在angular2及更高版本中完成此操作?

我尝试了Renderer2,但无法从组件创建指令

angular angular-directive angular-components angular-custom-validators
1个回答
0
投票
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. 
© www.soinside.com 2019 - 2024. All rights reserved.