聚焦后更新模型失去了对输入控制 [英] Updating model after focus is lost on input control

查看:67
本文介绍了聚焦后更新模型失去了对输入控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此code:

<input class="form-control" ng-model="actionText" />

AngularJS将更新模型actionText如在用户类型的东西,如果该模型被用于其他地方,例如在控制器,因为每个字符键入它被更新。我能想到的情况下,可能实际上是一件坏事。也许你只希望之后,用户必须已将焦点从输入到别的东西,比如当你需要验证输入时,它已经完成了模型值。我可以使用JavaScript的模糊功能设置一个处理程序来确定丢失焦点时,但似乎是反对角度,就是要的方式。是否有更新模型输入已失去焦点后的AngularJS方式?

AngularJS will update the model actionText as the user types stuff in. If this model is being used elsewhere, such as in a controller, it gets updated as each character is typed. I can think of situations where that could actually be a bad thing. Maybe you only want the model value AFTER the user has moved focus from the input to something else, such as when you need to validate the input when it has completed. I could use Javascript's blur function to setup a handler to determine when focus is lost but that seems to be against the way Angular is meant to be. Is there an AngularJS way of updating the model after the input has lost focus?

推荐答案

您可以使用此 ngModelOptions 指令。随着 ngModelOptions 您可以进一步细化 ngModel 指令是如何工作的。为了达到你的要求了,你可以使用这样的:

You can use the ngModelOptions directive for this. With ngModelOptions you can further refine how the ngModel directive works. To achieve what you asked for, you may use it like this:

<input class="form-control" ng-model="actionText" 
   ng-model-options="{ updateOn: 'blur'}"/>

您可以找到进一步的信息和角文档中工作示例:<一href=\"https://docs.angularjs.org/api/ng/directive/ngModelOptions\">https://docs.angularjs.org/api/ng/directive/ngModelOptions

You can find further information and a working example in the angular documentation: https://docs.angularjs.org/api/ng/directive/ngModelOptions

这篇关于聚焦后更新模型失去了对输入控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆