Textbox on changes directive

后端 未结 1 1257
我在风中等你
我在风中等你 2020-12-20 08:24

What is the equivalent for jQuery change event on every input in Angular2? Example:

$(\"input\").on(\'change\', function() { 
   console.log(\"*\"); 
});


        
相关标签:
1条回答
  • 2020-12-20 08:51

    You can handle it using Directive as said by Igor as below

    1. create a directive using

      import { Directive, HostListener, Renderer, ElementRef } from '@angular/core';
      @Directive({
          selector: '[change]'
      })
      export class ChangeDirective{
      
          constructor(
              private renderer: Renderer,
              private el: ElementRef
          ){}
      
          @HostListener('keyup') onKeyUp() {
      
           console.log('some thing key upped')
      
          }
      }
      
    2. Import it to the main.ts

    3. Add to declarations of the module

    LIVE DEMO

    0 讨论(0)
提交回复
热议问题