【问题标题】:AngularJS - custom filter with ngRepeat directiveAngularJS - 使用 ngRepeat 指令的自定义过滤器
【发布时间】:2013-03-02 16:35:25
【问题描述】:

我想将我的自定义过滤器与 ngRepeat 指令一起使用。这是我所拥有的

HTML:

<div ng-app="menuApp">
    <ul ng-controller="MenuCtrl">
        <li ng-repeat="item in menuItems | rootCategories">
            {{item.Name}}
        </li>
    </ul>
</div>

JS:

angular.module('menuApp', [])
    .filter('rootCategories', function() {
        return function(item) {
            return item.Parent == 0;
        };
    });

function MenuCtrl($scope) {
    $scope.menuItems = [{ "Id": 1, "Name": "Sweep", "Parent": 0 }];

    /*
    $scope.rootCategories = function(item) {
        return item.Parent == 0;
    };
    */
};

我不想使用注释掉的方式来过滤我的项目,因为真正的过滤器会比提供的示例复杂。由于某些原因,未定义输入参数“item”,因此我什么也看不到。你能告诉我有什么问题吗?谢谢。

【问题讨论】:

  • 我发现了以下评论:“更正:ng-repeat 中的自定义过滤器将整个关联数组作为输入传递,而不是每个对象的键/值,或 {{keyword.values} } 正如我之前所误解的那样。”但无法发明一种聪明的方式来应用它。
  • 我发现以下链接帮助我解决了我的问题:stackoverflow.com/questions/11753321/…

标签: angularjs angularjs-ng-repeat


【解决方案1】:

请看看这个小提琴。 http://jsfiddle.net/bKFXy/。 这使用将谓词对象作​​为过滤器表达式传递的语法。 item in menuItems | filter:{Parent:0}

还有另一种过滤方法,这里是http://jsfiddle.net/kd5dv/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 2013-06-11
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多