karojosh karojosh - 2 months ago 9
AngularJS Question

How to update ng-model on event click using $event in Angularjs

http://plnkr.co/ywhmyO

I have tried compile and directives but I'm not getting anywhere. the compile code in the example is ripped straight off the angular website but i've no idea how to use it. Any guidance much appreciated.

Answer

You are tripping over the fact that ng-repeat creates a new scope. So your updates are happening in the child scope only and not showing up in the parent scope.

You could do something like this in your view:

<div ng-repeat="name in names">
  <input ng-model="name.name" ng-click="changeName($index)" value="{{ name.name }}">
</div>

$index in the 0 based counter. Then in your controller:

$scope.changeName = function($index) {
    $scope.names[$index].name = $scope.nameselected;
};
Comments