4

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.

  • Could you please be a bit more specific on what you mean by 'compile and directives'? – Hengjie Dec 02 '12 at 09:59
  • check out the plnker, you'll see the prob straight away. many questions on angular data binding have focused on extending 'widget' type capabilities or including a jquery plugin. The angularjs website talks about directives here:http://docs.angularjs.org/guide/directive and compile here: http://docs.angularjs.org/api/ng.$compile. I understand I need to tell angular about the $event but not sure exactly how. –  Dec 02 '12 at 10:11

2 Answers2

10

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;
};
David B.
  • 87
  • 7
abhaga
  • 5,455
  • 2
  • 21
  • 20
  • 2
    no way! a thousand thanks. Reminds me of Saladin's scimitar slicing a silk scarf. –  Dec 02 '12 at 22:35
0

Why not pass arguments?

<div ng-repeat="name in names">
  <input ng-model="name.name" ng-click="changeName(name)" value="{{ name.name }}">
</div>
$scope.changeName = function(name) {
    name = $scope.nameselected;
};
sarah
  • 11
  • 1
    Because the argument will be passed by value/copy. Assigning it to something else has no effect on the outside scope. BUT - this is exactly what i wanted to do, to have a "generic" method that can be used on different "models". Any Idea how to achieve that without writing a whole directive? – icyerasor Jun 30 '16 at 17:18