2016-09-29 28 views
0

我有一个JSON文件是这样的:显示数据一旦

[ 
    { 
    "id": "1", 
    "name": "John", 
    "country": "Philippines" 
    }, 

    { 
    "id": "2", 
    "name": "Marsha", 
    "country": "Philippines" 
    }, 
    { 
    "id": "3", 
    "name": "Peter", 
    "country": "Philippines" 
    }, 
    { 
    "id": "4", 
    "name": "Kang", 
    "country": "Philippines" 
    } 
] 

我要的是在使用NG-重复显示,但因为所有国家都是一样的,我想显示该国只有一次。我怎么可能做到这一点?我的HTML文件是这样的:

<div ng-repeat="item in citizens"> 
    <label class="col-md-4 control-label" ng-model="item.id"> 
     {{item.id}} 
    </label> 
    <label class="col-md-4 control-label" ng-model="item.name"> 
     {{item.name}} 
    </label> 
    <label class="col-md-4 control-label" ng-model="item.country"> 
     {{item.country}} 
    </label> 
</div> 

感谢您的帮助。

+0

如果您想要显示每个国家一次,国家'菲律宾'应该是'id'和'name'? – Jens

+0

我只想显示国家只有一次,因为它们是相同的 – bleykFaust

+0

您可以使用groupBy:国家,我认为 – Jens

回答

0

尝试更改标签,如下:

<label class="col-md-4 control-label" ng-model="item.country" ng-show="$index>0 && item.country!=citizens[$index-1].country"> 
    {{item.country}} 
</label> 

的NG-节目将检查上一个项目的国家是不一样的当前项目的国家,则仅显示标签。 注意:$index>0将确保跳过第一项检查。

0

在显示国家名称的标签上添加ng-if条件。

<label class="col-md-4 control-label" ng-bind = "item.country" 
    ng-model="item.country" ng-if="$index>0 && item.country!=citizens[$index-1].country"> 
    </label> 

这里是plunker example

+0

感谢这个蹲点,但它没有显示“菲律宾”我需要显示国家的价值一次,因为他们都是从相同的国家 – bleykFaust

+0

@bleykFaust编辑它显示国家 – GANI

+0

它仍然是一样的。它并没有将国家“菲律宾”列为头球 – bleykFaust

0

为了实现你必须通过过滤器的角度NG重复使用该组的需求。

我已经连接一起剪断的代码

<div ng-repeat="(key, value) in citizens | groupBy: 'country'"> 
    Group name: {{ key }} 
    <div ng-repeat="item in value"> 
     <div>Name: {{ item.name }}</div> 
    </div> 
</div> 

你必须包括在脚本角过滤文件中。

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.11/angular-filter.min.js"></script> 

而且注入角度滤波依赖于你的屁股像这样

angular.module('myApp', ['angular.filter']); 

我认为这是你在找什么。请查看this了解更多详情