karojosh karojosh - 1 month ago 4x
AngularJS Question

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


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.


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 }}">

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

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