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
controllerAs
pattern, you should be usingcontroller
alias while accessing any variable from controller function. But that should be bounded tothis
context ofcontroller
.Demo Here
Extended
Also keep in mind while using
this
keyword inside a controller factory function. You should assignthis
variable to some variable to ensure thatthis
which you are using is not otherthis
, look atthis
context related issue.