typescript 如何在角度 2 中调用输入文本更改函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48083525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:09:00  来源:igfitidea点击:

How to call function on input text change in angular 2

typescriptangular2-templateangular2-components

提问by Usf Noor

I want to call the type script function on input change of text box, and its working for me. But it call on every change, I want to call when the minimum number of character in the text box are 5.

我想在文本框的输入更改时调用类型脚本函数,它对我有用。但是每次更改都会调用,我想在文本框中的最小字符数为 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);
    }
}

回答by Arshmeet

You can write something like this

你可以写这样的东西

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

You can also use template reference variable like this

您还可以像这样使用模板引用变量

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