我从AngularJS的W3Schools教程中得到了这个例子。我通过将复选框span的值绑定到使用表达式进行了一些小改动。我认为待办事项列表不会再更新。但它仍然如此。导致ng-repeat
被解雇的原因是因为我添加了待办事项?
http://plnkr.co/edit/Kojz2ODWDS8dFDNzjYR5?p=preview
<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body ng-app="myApp" ng-controller="todoCtrl">
<h2>My Todo List</h2>
<form ng-submit="todoAdd()">
<input type="text" ng-model="todoInput" size="50" placeholder="Add New">
<input type="submit" value="Add New">
</form>
<br>
<div ng-repeat="x in todoList">
<input type="checkbox" ng-model="x.done"> <span>{{x.todoText}}</span>
</div>
<p><button ng-click="remove()">Remove marked</button></p>
<script>
var app = angular.module('myApp', []);
app.controller('todoCtrl', function($scope) {
$scope.todoList = [{todoText:'Clean House', done:false}];
$scope.todoAdd = function() {
$scope.todoList.push({todoText:$scope.todoInput, done:false});
$scope.todoInput = "";
};
$scope.remove = function() {
var oldList = $scope.todoList;
$scope.todoList = [];
angular.forEach(oldList, function(x) {
if (!x.done) $scope.todoList.push(x);
});
};
});
</script>
</body>
</html>
答案 0 :(得分:1)
单击Add New
按钮会提交相应的表单,并使用ng-submit="todoAdd()"
它将调用此函数。这反过来会为您的范围中的todoList
添加一个条目。由于此数组已被修改,因此会触发角度摘要循环并更新列表。
对你的问题的一些建议:首先,你的意思是W3Schools,而不是W3C(这是一个标准化组织,通常没有做教程,这就是为什么我有古玩 - 而且,你会发现很多理由为什么不goolgin或看meta时使用W3Schools。此外,如果您与其他一些代码进行比较,您应该包含它或至少链接到它。
我是通过Google搜索找到的,似乎您唯一的更改是使用<span>{{x.todoText}}</span>
代替<span ng-bind="x.todoText"></span>
。这里的摘要周期确实没有区别。唯一的区别是,通过使用{{}}
,在实际替换变量之前,它可能首先在浏览器窗口中呈现为大括号。因此,通常最好使用ng-bind
。