更改指令上的文本框

问题描述:

在Angular2的每个输入上,jQuery更改事件的等效项是什么? 示例:

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

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

您可以使用Igor所说的伪指令"来处理它

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

  1. 使用

  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')

    }
}

  • 将其导入main.ts

  • Import it to the main.ts

    添加到模块的声明中

    实时演示

    LIVE DEMO