角度指令-对主机输出做出反应

时间:2018-10-22 17:30:12

标签: angular angular2-directives

我有一个用于输入字段的指令。

<input myDirective [ngModel]="model" (ngModelChange)="doSomething()">

现在在“ myDirective”中,我想对输入“ ngModelChange” -Output做出反应。

背景:如果输入不为空,则伪指令会对输入执行某些操作。 现在,我尝试使用@HostListener来获取事件并检查模型是否包含任何内容。但是:如果我在父组件中设置模型,则该解决方案将无法工作,因为在输入上没有触发任何事件。

完整的最小示例:

模板:

  <input myDirective [ngModel]="model" (ngModelChange)="model = $event; someFunction()">

指令:

@Input() model: any
fun reactToChanges(){
    if(this.model.length > 0){doSomething}
}

我当前的解决方案:

@HostListener('input', ['$event']) onEvent($event) {
   this.reactToChanges()
}

父组件:

fun foo(newValue){
   //this does not trigger the directives function, but it has to
   this.model = newValue
}

如果foo被触发,则@HostListener当然不会消失。

0 个答案:

没有答案