Detect when input value changed in directive

后端 未结 4 1855
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  Happy的楠姐
    2021-02-03 18:24

    You can also use HostListener. For more information about HostListener, you can go through this link. Here is the code.

    import {Directive, ElementRef, HostListener} from '@angular/core';
    
    
    @Directive({
           selector: '[number]'
     })
    
    export class NumberDirective {
    
        @Input() public number: any;
        @Input() public input: any;
    
        constructor(private el: ElementRef) {}
    
        @HostListener('change') ngOnChanges() {
            console.log('test');
        }
    
    }
    

提交回复
热议问题