Ng-repeat过滤当前迭代值

时间:2014-05-28 10:22:51

标签: angularjs

是否可以做一个简单的过滤器,像这样?

<div col ng-repeat="v in venues | filter:v.matches.loose">

所以如果v.matches.loose的值为真,它只会重复这一行吗?

我有几个不同的情况,这将是有用的,我不想为我需要的每个案例定义app.filter('...

编辑:重复的完整HTML

<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" ng-repeat="v in venues|filter:{matches.loose:true}">

            <div row>
                <div col lg="4" md="4">
                    {{v.name}}        
                </div>
                <div col lg="4" md="4">
                    Loose match: <span ng-show="v.matches.loose">Yes</span><span ng-hide="v.matches.loose">No</span>
                </div>
                <div col lg="4" md="4">
                    Strict match: <span ng-show="v.matches.strict">Yes</span><span ng-hide="v.matches.strict">No</span>
                </div>

            </div>
        </div>

2 个答案:

答案 0 :(得分:1)

<div col ng-repeat="v in venues | filter:{matches.loose:true} ">
如果您的代码符合以下结构,则

应该有效:

<script src="//cdn.jsdelivr.net/angularjs/1.2.9/angular.min.js"></script>

<body ng-app ng-controller="Main">
    <div ng-repeat="v in venues|filter:{match.loose:true}">
        {{v.name}}
    </div>
    <script>
        function Main($scope) {
            $scope.venues= [{
                match: {
                    loose: true
                },
                name: "foo"
            }, {
                match: {
                    loose: false
                },
                name: "bar"
            }, ]
        }
    </script>
</body>

证明:

http://jsfiddle.net/camus/8Lx96/1/

答案 1 :(得分:1)

上述答案的替代方法是使用 ng-if ,如下所示:

<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" ng-repeat="v in venues">
    <div ng-if="v.matches.loose === true">
        <div row>
            <div col lg="4" md="4">
                {{v.name}}        
            </div>
            <div col lg="4" md="4">
                Loose match: <span ng-show="v.matches.loose">Yes</span><span ng-hide="v.matches.loose">No</span>
            </div>
            <div col lg="4" md="4">
                Strict match: <span ng-show="v.matches.strict">Yes</span><span ng-hide="v.matches.strict">No</span>
            </div>
        </div>
    </div>
</div>