刚刚在看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
Although the reason has been given upstairs, I guess you don’t quite understand it. Let me give you a document to see what $watch
is. The document is here.
阿神2017-05-15 17:00:57
$scope.$watch('TC.location.path()',function (path) {
TC.Filters = {'/active':{completed: false},'/completed':{completed:true}}[path];
});
The change of the path is monitored here. When the path changes, the parameters of the filter also change. In fact, it is the switching between completed and active data