I was just looking at the angularjs todoMVC project and found that it customized a filter in the controller
$scope.$watch('TC.location.path()',function (path) {
TC.Filters = {'/active':{completed: false},'/completed':{completed:true}}[path];
});
Afterwards in view
<li ng-repeat="todo in TC.todos | filter:TC.Filters track by $index" ng-class="{completed: todo.completed, editing: todo === TC.editedTodo}">
My problem is mainly that I don’t understand the way the first piece of code is written
function (path) {
TC.Filters = {'/active':{completed: false},'/completed':{completed:true}}[path];
}
In this method, after passing in the path, different filters are selected based on the path.
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.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