AngularJS:使用 jQuery 更改时,ng-model 绑定不会更新 [英] AngularJS : ng-model binding not updating when changed with jQuery

查看:33
本文介绍了AngularJS:使用 jQuery 更改时,ng-model 绑定不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 HTML:

<input id="selectedDueDate" type="text" ng-model="selectedDate" />

当我在框中键入内容时,模型会通过 2 路绑定机制进行更新.甜.

When I type into the box, the model is updated via the 2-way-binding mechanism. Sweet.

但是当我通过 JQuery 执行此操作时...

However when I do this via JQuery...

$('#selectedDueDate').val(dateText);

它不会更新模型.为什么?

It doesn't update the model. Why?

推荐答案

Angular 不知道这个变化.为此,您应该调用 $scope.$digest() 或在 $scope.$apply() 中进行更改:

Angular doesn't know about that change. For this you should call $scope.$digest() or make the change inside of $scope.$apply():

$scope.$apply(function() { 
   // every changes goes here
   $('#selectedDueDate').val(dateText); 
});

请参阅以更好地了解脏检查

更新:这里是一个例子

这篇关于AngularJS:使用 jQuery 更改时,ng-model 绑定不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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