Home  >  Q&A  >  body text

angular.js - 关于angularjs中自定义filter的问题

刚刚在看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选择不同的过滤器。

我想问问有没有文档是介绍这种在过滤器对象内选择的写法的。

大家讲道理大家讲道理2738 days ago509

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