2

我有一個學校列表和一個教師列表。在視圖中使用的角度屬性(變量)

學校

[ 
    { 
    "id": 1, 
    "nameOfInstitution": "Summer Elementary", 
    "schoolLevel": "01304" 
    }, 
    { 
    "id": 2, 
    "nameOfInstitution": "Grady Middle", 
    "schoolLevel": "02400" 
    } 
    ] 

教師

[ 
{ 
    "id": "1", 
    "school": "1", 
    "name": { 
    "lastOrSurname": "HARRISON", 
    "firstName": "JOHN", 
    "middleName": "" 
    } 
}, 
{ 
    "id": "2", 
    "school": "1", 
    "name": { 
    "lastOrSurname": "Nelson", 
    "firstName": "Darren", 
    "middleName": "" 
    } 
}, 
{ 
    "id": "3", 
    "school": "2", 
    "name": { 
    "lastOrSurname": "Stewart", 
    "firstName": "Manuel", 
    "middleName": "" 
    } 
}] 

在控制器我有 ...

$scope.schools = schools; 
$scope.teachers = teachers; 

問題是相關的觀點。我怎樣才能做到這一點?

  • 夏季小學
    • 約翰·哈里森
    • 達倫·納爾遜
  • 格雷迪中東
    • 曼努埃爾·斯圖爾德

我一直在使用「ng-repeat」指令,但是我怎樣才能顯示屬於每個學校的教師?

謝謝!

回答

1

您可以在NG-重複過濾教師:

<div ng-repeat="school in schools"> 
    <div ng-repeat="teacher in teachers | filter: {school: school.id}"> 

但一定要添加id屬性由教師使用的每個學校。

或者如果teacher.school性質的學校排列在學校的指數,你可以這樣做:

<div ng-repeat="school in schools"> 
    <div ng-repeat="teacher in teachers | filter: {school: $index}"> 

最後需要說明:這會工作,但如果你有很多學校和老師,它會更高性能執行從JavaScript過濾你顯示它之前,即建立一個像

schools = [{ 
    nameOfInstitution: ..., 
    schoolLevel: ..., 
    teachers: [{ 
    lastOrSurname: ... 
    }] 
    // ... etc 
}]; 
+0

filter:{school:school.id}是錯的( – 2015-04-01 17:52:19

+0

@PetrAveryanov爲什麼它錯了?確實需要爲每個學校增加一個「ID」。當學校id是他們的指數時,我舉了另一個例子。 – floribon 2015-04-01 17:53:50

+0

是的,對不起!我的意思是在學校json中顯示id。感謝您的快速回復! – 2015-04-01 17:54:18

0

使用過濾器的結構:

<div ng-repeat="school in schools"> 
    {{school.nameOfInstitution}} 
<p ng-repeat="teacher in teachers | filter:{school: ''+ ($index+1)}:true"> 
    {{teacher.name.lastOrSurname}} 
</p> 
</div> 

但你最好有一些學校的ID ...