angular.js - Questions about custom filters in angularjs
大家讲道理
大家讲道理 2017-05-15 16:58:57
0
2
521

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.

I would like to ask if there is any documentation that introduces this method of selecting within a filter object.

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
伊谢尔伦

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.

阿神
$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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template