如何在角度2中调用输入文本更改的功能

时间:2018-01-03 18:59:08

标签: typescript angular2-template angular2-components

我想在输入更改文本框时调用类型脚本函数,它对我有用。但它会调用每个更改,我想在文本框中的最小字符数为5时调用。

export class StudentMaintenanceComponent implements OnInit {
    @Component({
        selector: 'app-student-management',
        template: `<input placeholder="" class="form-control" type="text" [maxlength]="5" (input)="SearchData($event.target.value)">`,
        styleUrls: ['./app-student-management.css']
    })

    SearchData(_no: string) { // should be called when minimum no of character are 5 in the text field.
        console.log(_no);
    }
}

1 个答案:

答案 0 :(得分:3)

你可以写这样的东西

(input)="$event.target.value.length > 5 && SearchData($event.target.value)"

你也可以像这样使用模板引用变量

<input placeholder="" #textInput class="form-control" type="text" [maxlength]="5" (input)="textInput.value.length > 5 && SearchData(textInput.value)">