2016-09-29 24 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瞭解更多詳情