angularjs ng-change事件应仅在键入完成后调用

voj3qocg  于 2022-12-10  发布在  Angular
关注(0)|答案(3)|浏览(200)

我有一个带有ng-change事件的文本字段。只有当用户完成键入时才应该调用该事件。如何延迟调用该事件?

<input type="text" ng-model="custname" ng-change="findCustName()">
3wabscal

3wabscal1#

You should use ng-blur instead
A blur event fires when an element has lost focus.

<input type="text" ng-model="custname" ng-blur="findCustName()">

If you do not want to use ng-blur or lost focus, you can use the same ng-change with ng-model-options
As of Angular 1.3, you could use Angular ng-model-options directive

<input ng-change="findCustName()" ng-model-options="{debounce:1000}">
1l5u6lss

1l5u6lss2#

When I want to call a function when typing is completed I use ng-keyup .
Using ng-keyup your function will be call every time user press a key.
I know your asking when user finish typing , but let me explain why I prefer using ng-keyup and not ng-blur
If I have to call a validation function, ng-blur won't help, because you will have to focus out to launch the function. Using ng-keydown , function will be launch. so if user finished typing it will be launch also

acruukt9

acruukt93#

我用这个函数在输入新值后调用。

ng-model-options="{ updateOn: 'blur' }" watch-selection

相关问题