search

Home  >  Q&A  >  body text

angular.js - Questions about custom filters in angularjs

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.

大家讲道理大家讲道理2758 days ago528

reply all(2)I'll reply

  • 伊谢尔伦

    伊谢尔伦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.

    reply
    0
  • 阿神

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

    reply
    0
  • Cancelreply