Heim > Fragen und Antworten > Hauptteil
刚刚在看angularjs的todoMVC项目,发现它在控制器中自定义了一个过滤器
$scope.$watch('TC.location.path()',function (path) {
TC.Filters = {'/active':{completed: false},'/completed':{completed:true}}[path];
});
之后在视图中
<li ng-repeat="todo in TC.todos | filter:TC.Filters track by $index" ng-class="{completed: todo.completed, editing: todo === TC.editedTodo}">
我的问题主要是第一段代码这种写法不了解
function (path) {
TC.Filters = {'/active':{completed: false},'/completed':{completed:true}}[path];
}
这个方法中传入path后根据path选择不同的过滤器。
阿神2017-05-15 17:00:57
$scope.$watch('TC.location.path()',function (path) {
TC.Filters = {'/active':{completed: false},'/completed':{completed:true}}[path];
});
这里监听了路径的变化,当路径变化了filter的参数也随之变化了。实际上就是completed和active数据的切换