我正在使用ng-model-options来防止我的文本输入模型被更新,直到焦点丢失.那部分都有效.但是,我需要在文本输入失去焦点并更新模型后处理事件.在那种情况下,我需要访问新模型的更新值,而不是旧模型.
我试图使用ng-blur事件,但是,在我的处理程序中,值是旧值,而不是输入的新值.我做错了什么或者在文本输入失去焦点后是否有更好的方法来获取新提交的模型值?
<input type="text"
ng-model-options="{ updateOn: 'blur', debounce:0 }"
ng-model="vm.myModel.amount"
ng-blur="vm.handleBlurEvent(vm.myModel.amount)" />
vm.handleBlurEvent = function(item)
{
//alert(item.amount); //Old amount - not new amount entered
}
Run Code Online (Sandbox Code Playgroud)
Chr*_*ler 16
这是由用户1750537的先前响应重新设计,延迟模型更新直到控制模糊,然后处理更改一次,不需要去抖动,并且模型值将在更改逻辑之前按预期设置.
<input type="text"
ng-model-options="{ updateOn: 'blur' }"
ng-model="vm.myModel.amount"
ng-change="vm.handleChangeEvent(vm.myModel.amount)" />
vm.handleChangeEvent = function(item)
{
alert(item.amount); //new shows the NEW amount :-)
}
Run Code Online (Sandbox Code Playgroud)
感谢这些建议。实际上,我通过向 ng-model-options 的 updateOn 属性添加 1 个选项来设法使其正常工作。通过添加“默认”一切正常!
ng-model-options =“{updateOn:'默认模糊}”就是诀窍。
<input type="text"
ng-model-options="{ updateOn: 'default blur' }"
ng-model="vm.myModel.amount"
ng-blur="vm.handleBlurEvent(vm.myModel.amount)" />
vm.handleBlurEvent = function(item)
{
//alert(item.amount); //new shows the NEW amount :-)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18027 次 |
| 最近记录: |