AngularJS : ng-model binding not updating when changed with jQuery

Greg picture Greg · Aug 8, 2012 · Viewed 183.6k times · Source

This is my HTML:

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

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

However when I do this via JQuery...

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

It doesn't update the model. Why?

Answer

Renan Tomal Fernandes picture Renan Tomal Fernandes · Aug 9, 2012

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

UPDATE: Here is an example