Detect when input value changed in directive

后端 未结 4 1802
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-03 17:48

I\'m trying to detect when the value of an input changed in a directive. I have the following directive:

         


        
4条回答
  •  醉梦人生
    2021-02-03 18:15

    You need to make an input property of input and then use the ngOnChanges hook to tell when the input property changes.

    @Directive({
        selector: '[number]'
    })
    export class NumberDirective implements OnChanges {
        @Input() public number: any;
        @Input() public input: any;
    
        ngOnChanges(changes: SimpleChanges){
          if(changes.input){
            console.log('input changed');
          }
        }
    }
    

    Plunkr

    Stackblitz

提交回复
热议问题