使用ng-repeat跳过某些条目

时间:2015-08-19 03:59:53

标签: angularjs

我已经设置了$watch这样:

ng-repeat

我的mods对象如下所示:

<div id="popular" ng-repeat="m in mods">
   <!-- stuff here -->
</div>

<div id="personal" ng-repeat="m in mods">
   <!-- stuff here -->    
</div>

我希望第一个div中的ng-repeat仅在var mods = [ {id:1232, group:'popular', ....}, {id:1231232, group:'personal', ...} {id:544, group:'personal', ...} ] 时创建元素,这可能吗?

1 个答案:

答案 0 :(得分:1)

你可以做点什么,

<div ng:app="myApp">
    <div ng-controller="HelloCntl">
        <div id="popular" ng-repeat="m in mods | weDontLike: 'personal'">   
            {{m}}
        </div>
    </div>
</div>


angular.module('myApp', []).filter('weDontLike', function () {

    return function (items, name) {

        var arrayToReturn = [];

        for (var i = 0; i < items.length; i++) {
            if (items[i].group != name) {
                arrayToReturn.push(items[i]);
            }
        }

        return arrayToReturn;
    };
});

function HelloCntl($scope) {
    $scope.mods = [{
        id: 1232,
        group: 'popular'
    }, {
        id: 1231232,
        group: 'personal'
    }, {
        id: 544,
        group: 'personal'
    }]
}

或者

<div ng:app="myApp">
    <div ng-controller="HelloCntl">
        <div id="popular" ng-repeat="m in mods | filter: {group : 'personal'}">{{m}}</div>
    </div>
</div>

Demo