【问题标题】:Is it possible passing directive to the event handler in angular?是否可以将指令以角度传递给事件处理程序?
【发布时间】:2019-04-02 01:52:28
【问题描述】:

角度是否可以这样做:

//template file
<input *ngFor="let elem in elements" [myDirective]="elem" 
      (keyup.enter)="onEnter(myDirective)">

//.ts file
onEnter(dir: MyDirective){
   //access myDirective instanse here
}

我知道我可以通过@ViewChildren 做到这一点,但我想确保可以直接通过吗?

【问题讨论】:

    标签: angular


    【解决方案1】:

    在你的指令中,确保你设置了exportAs:

    @Directive({
     selector: '[myDirective]',
     exportAs: 'myDirective'
    })
    

    然后,你可以在模板中获取到它的引用:

    <input *ngFor="let elem in elements" [myDirective]="elem" #test="myDirective"
      (keyup.enter)="onEnter(test)">
    

    这里我叫它test,但你可以随便叫它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-03
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-01
      • 1970-01-01
      • 2014-01-24
      相关资源
      最近更新 更多