Cla*_*las 4 javascript angularjs angularjs-controller angularjs-controlleras
我想使用函数更改ng-repeat循环中项目的值.
例如这不起作用.
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>
Run Code Online (Sandbox Code Playgroud)
JS
$scope.example = function(v) {
v.name = 'i am different now';
};
Run Code Online (Sandbox Code Playgroud)
完整的例子
Pan*_*kar 10
使用controllerAs模式时,您应该在controller从控制器功能访问任何变量时使用别名.但这应该受到this背景的限制controller.
<button ng-click="todoList.example(todo)">Click me</button>
Run Code Online (Sandbox Code Playgroud)
扩展
在this控制器工厂函数中使用关键字时也要记住.您应该将this变量分配给某个变量以确保this您使用的变量不是其他变量this,请查看与this上下文相关的问题.
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';
};
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1906 次 |
| 最近记录: |