如何逃避ng-Repeat循环?

时间:2015-08-27 20:33:23

标签: angularjs angularjs-ng-repeat

我有以下ng-repeat循环将JSON数据解压缩到Accordion:

<accordion close-others="oneAtATime">
    <accordion-group ng-repeat="service in userPF.custom.services">
        <accordion-heading><input type="checkbox" name="status" disabled>On <input type="checkbox" name="status" disabled checked>Off {{service.name}}</accordion-heading>
        Related Items:<br>
        <div class="secondary" ng-repeat="fields in userPF.custom.fields">
            <span ng-show="checkForMatch(service.name, fields.services)">
                <span ng-repeat="value in fields.values">
                    {{value.name}}<br>
                </span>
            </span>
        </div>
    </accordion-group>
</accordion>

第一次循环{{service.name}}将始终返回数据,但第二个值{{value.name}}可能不会。如果不是,我不希望div或span渲染,只是标题。我已经尝试了一些关于ng-if的变化但是还没有接近能够判断我是否正在获取数据。

1 个答案:

答案 0 :(得分:0)

参考此链接。 如此代码显示并且其工作正常,您可以在ng-repeat内部进行ng-show。

<div ng-app ng-controller="RepeatTestCtrl">
    <ul ng-repeat="item in items">
        <li ng-show="item.id.length == 0">{{item.name}} is even</li>
        <li ng-show="item.id % 2 == 1">{{item.name}} is odd</li>
    </ul>
</div>

function RepeatTestCtrl($scope) {
    $scope.items = [
        { id: 1, name: 'test 1' },
        { id: 2, name: 'test 2' },
        { id: 3, name: 'test 3' },
        { id: 4, name: 'test 4' }
    ];
}

http://jsfiddle.net/b004t06z/41/