I've written this little plugin for jQuery which will make all calls to .val(value)
update the angular element if present:
(function($, ng) {
'use strict';
var $val = $.fn.val; // save original jQuery function
// override jQuery function
$.fn.val = function (value) {
// if getter, just return original
if (!arguments.length) {
return $val.call(this);
}
// get result of original function
var result = $val.call(this, value);
// trigger angular input (this[0] is the DOM object)
ng.element(this[0]).triggerHandler('input');
// return the original result
return result;
}
})(window.jQuery, window.angular);
Just pop this script in after jQuery and angular.js and val(value)
updates should now play nice.
Minified version:
!function(n,t){"use strict";var r=n.fn.val;n.fn.val=function(n){if(!arguments.length)return r.call(this);var e=r.call(this,n);return t.element(this[0]).triggerHandler("input"),e}}(window.jQuery,window.angular);
Example:
// the function_x000D_
(function($, ng) {_x000D_
'use strict';_x000D_
_x000D_
var $val = $.fn.val;_x000D_
_x000D_
$.fn.val = function (value) {_x000D_
if (!arguments.length) {_x000D_
return $val.call(this);_x000D_
}_x000D_
_x000D_
var result = $val.call(this, value);_x000D_
_x000D_
ng.element(this[0]).triggerHandler('input');_x000D_
_x000D_
return result;_x000D_
_x000D_
}_x000D_
})(window.jQuery, window.angular);_x000D_
_x000D_
(function(ng){ _x000D_
ng.module('example', [])_x000D_
.controller('ExampleController', function($scope) {_x000D_
$scope.output = "output";_x000D_
_x000D_
$scope.change = function() {_x000D_
$scope.output = "" + $scope.input;_x000D_
}_x000D_
});_x000D_
})(window.angular);_x000D_
_x000D_
(function($){ _x000D_
$(function() {_x000D_
var button = $('#button');_x000D_
_x000D_
if (button.length)_x000D_
console.log('hello, button');_x000D_
_x000D_
button.click(function() {_x000D_
var input = $('#input');_x000D_
_x000D_
var value = parseInt(input.val());_x000D_
value = isNaN(value) ? 0 : value;_x000D_
_x000D_
input.val(value + 1);_x000D_
});_x000D_
});_x000D_
})(window.jQuery);
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<div ng-app="example" ng-controller="ExampleController">_x000D_
<input type="number" id="input" ng-model="input" ng-change="change()" />_x000D_
<span>{{output}}</span>_x000D_
<button id="button">+</button>_x000D_
</div>
_x000D_
This answer was copied verbatim from my answer to another similar question.