我正在关注'http://angularjs.org/'的基本AngularJS教程,但我对某些功能被触发的原因感到有些困惑。
他们的待办事项列表应用程序,他们在控制器中有以下JS:
$scope.remaining = function() {
var count = 0;
angular.forEach($scope.todos, function(todo) {
count += todo.done ? 0 : 1;
});
return count;
};
链接到以下HTML:
<div ng-controller="TodoCtrl">
<span>{{remaining()}} of {{todos.length}} remaining</span>
[ <a href="" ng-click="archive()">archive</a> ]
<ul class="unstyled">
<li ng-repeat="todo in todos">
<input type="checkbox" ng-model="todo.done">
<span class="done-{{todo.done}}">{{todo.text}}</span>
</li>
</ul>
</div>
但是,当选中/取消选中复选框时,我看不到触发$ scope.remaining函数的方式/位置,然后更新UI中的值。还有其他范围函数,但在这种情况下似乎没有调用它们,那么这个函数有什么特别之处呢?
答案 0 :(得分:0)
函数remaining
用于表达式(例如{{}}
)angular为这些表达式创建监视。因此,每当发生摘要循环时,您的函数都会被调用。