草庐IT

javascript - Angular : ng-model binding not updating when changed with jQuery

coder 2023-07-03 原文

这是我的 HTML:

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

当我在框中键入内容时,模型会通过双向绑定(bind)机制进行更新。甜蜜。

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

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

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

最佳答案

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

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

参见 this更好地理解脏检查

更新:Here是一个例子

关于javascript - Angular : ng-model binding not updating when changed with jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11873627/

有关javascript - Angular : ng-model binding not updating when changed with jQuery的更多相关文章

随机推荐