admin管理员组文章数量:1394395
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
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
http://plnkr.co/edit/kobMJCsj4bvk02sveGdG
Share Improve this question edited Feb 26, 2016 at 11:43 Pankaj Parkar 136k23 gold badges240 silver badges303 bronze badges asked Feb 26, 2016 at 11:28 Claudio Ɯǝıs MulasClaudio Ɯǝıs Mulas 1,1123 gold badges15 silver badges24 bronze badges1 Answer
Reset to default 10When using controllerAs
pattern, you should be using controller
alias while accessing any variable from controller function. But that should be bounded to this
context of controller
.
<button ng-click="todoList.example(todo)">Click me</button>
Demo Here
Extended
Also keep in mind while using this
keyword inside a controller factory function. You should assign this
variable to some variable to ensure that this
which you are using is not other this
, look at this
context related issue.
angular.module('todoApp', [])
.controller('TodoListController', function() {
var toList = this;
toList.todos = [{name:'test 1'},{name:'test 2'}];
toList.example = function(v) {
v.name = 'ora bene';
};
});
本文标签: javascriptChange a value inside ngrepeat cycle with ngclickStack Overflow
版权声明:本文标题:javascript - Change a value inside ng-repeat cycle with ng-click - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744623715a2616186.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论