AngularJS:NG-模型时使用jQuery改变绑定不更新

问题描述:

这是我的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 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); 
});

请参阅以更好地理解的脏检查

See this to better understand dirty-checking

更新这里是一个例子。