[ACCEPTED]-How to detect input element change when set value in angularjs-angularjs

Accepted answer
Score: 14

First of all, two-way data binding is here 7 in order to avoid such scenarios.

 <input ng-model='ctrl.field' />

 <button ng-click='ctrl.field = "new value"'>change</button>

Second, there 6 is ng-change directive that works together with ng-model, which 5 can be used to do something when value in 4 the model changed.

 <input ng-model='ctrl.field' ng-change='alert("changed!")' />

If you still want to change 3 value outside of angular directly with DOM, just 2 fire event that angular listen to - blur or 1 keypressed

 <button ng-click='$("#id").val(a); $("#id").trigger("blur")'>change</button>


 <button ng-click='angular.element("#id").val(a); angular.element("#id").trigger("blur")'>change</button>
Score: 2

Updated fiddle.

You are not supposed to use querySelector (just like 7 you are not supposed to use jQuery) with 6 Angular, unless you have a real good reason 5 to.

You can just modify $scope.data.test, and your text box 4 contents will be updated automatically. In 3 order for this to work, you also need the 2 button to have the same controller (so they share 1 scope), and use ng-click instead of onclick, like so:

<div ng-app="myDirective" ng-controller="x">
    <input id="angular" type="text" ng-model="data.test" my-directive=""></input>
    <button ng-click="data.test = 'testg';">click</button>

More Related questions