I want to change a value of an item inside an ng-repeat cycle using a function.
This for example won't work.
HTML
<ul class="unstyled">
<li ng-repeat="todo in todoList.todos">
<span>{{todo.name}}</span>
<button ng-click="example(todo)">Change me</button>
</li>
</ul>
JS
$scope.example = function(v) {
v.name = 'i am different now';
};
Full example
When using
controllerAspattern, you should be usingcontrolleralias while accessing any variable from controller function. But that should be bounded tothiscontext ofcontroller.Demo Here
Extended
Also keep in mind while using
thiskeyword inside a controller factory function. You should assignthisvariable to some variable to ensure thatthiswhich you are using is not otherthis, look atthiscontext related issue.