更改NG-重复周期内的价值与NG-点击 [英] Change a value inside ng-repeat cycle with ng-click
问题描述
我想用一个功能来改变纳克重复周期内的项的值。
I want to change a value of an item inside an ng-repeat cycle using a function.
例如,这是行不通的。
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';
};
完整的示例
http://plnkr.co/edit/kobMJCsj4bvk02sveGdG
推荐答案
在使用 controllerAs
模式,你应该使用控制器
的别名,而从访问控制器功能的任何变量。但应该是有界到控制器
。
When 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>
扩展
也请记住,而使用控制器工厂函数这里面
关键字。你应该分配这个
变量一些变量,以确保这个
你正在使用的不是其他这个
,看 这个
背景相关的问题 。
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';
};
});
这篇关于更改NG-重复周期内的价值与NG-点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!