简体   繁体   中英

Angularjs 1.3 asynchronous validation trigger on `enter`

Angularjs 1.3 has an option to asynchronously validate a field through $asyncValidators . There is also an option to set debounce and updateOn (through ngModelOptions ) to decide when to trigger validation. I have a requirement to trigger validation on enter (keyCode == 13). Is this easily possible with the above options? or what is the best way.

I see an example of using asyncValidator here . But not sure how to filter and only trigger the validation for keyCode = 13.

Thanks. bsr.

updateOn and debounce are not for triggering validation, but to define when the model should be updated (validators run at this point, of course). Eg

<input type="text" ng-model="stuff" ng-model-options="{ updateOn: 'blur' }" />

When you enter something into this field, then stuff won't get updated. It's only updated when the input field loses its focus. Even if there was a way to specify a key (you can only specify event types), then you'd have the problem that the model would never get updated if someone doesn't press enter . That's quite a risk only to have the text validated on enter .

One solution is to set ng-model-options="{ updateOn: 'blur' }" , attach an event listener to the input field and blur the field on enter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM