【发布时间】:2015-11-12 00:01:06
【问题描述】:
假设students 的一些数据在不同schools 的类(groups)中:
{
"students": {
"alexa" : {
"firstName" : "ReJina",
"grade" : 2,
"lastName" : "idk",
...
},
"bobby" : { ... },
"chris" : { ... },
...
},
"schools": {
"springfield" : {
"name" : "Springfield Elementary",
"location" : "123 Main St",
...
},
"quahog" : { ... },
"zion" : { ... },
...
},
"group": {
"one" : {
"members" : {
"alexa" : true,
"bobby" : true,
"chris" : true
},
"school" : {
"springfield" : true
},
"name" : "Math Class"
},
"two" : {
"members" : {
"bart" : true,
"lisa" : true,
"meg" : true,
...
},
"school" : {
"quahog" : true
},
"name" : "Art Class"
},
"three" : { ... },
"four" : { ... },
...
}
}
据我所知,这是以扁平方式构建数据的正确方法。如果这不正确,请告诉我。我在每个项目上保留相关列表,因此每个班级都需要一个名册 (members),如果我还遇到学生在多个班级 (group) 或学校的情况,则需要班级列表以及他们就读的学校。
现在在HTML 中,我们需要一个视图来显示所有学校班级 (groups) 和每个班级中的学生以及嵌套在同一视图中的学生ng-repeat:
<div id="data-table-div" class="col-md-6">
// Get all the groups, and show them one by one
<div ng-repeat="group in groups">
// This works - How do I get the school it is associated to?
// ??
{{group.name}} - {{group's school's name}}
//Start a table
<table>
<thead> <tr> <th>Student Name</th> <th>Grade</th> ... </thead>
<tbody>
// Get the students in each group, and show them one by one
// ??
<tr ng-repeat="student in groups.members">
<td>{{student.name}}</td> <td>{{student.grade}}</td> ...
</tr>
</tbody>
</table>
</div>
</div>
在控制器中:
angular.module('sofDataViewerApp')
.controller('dataTableController', function ($scope, $firebaseObject, $firebaseArray) {
// This gets all of our groups. It also allows `group.memebers` to render the appropriate number of `<tr>`s as it knows it has N students.
var groupsRef = new Firebase("https://sof-data.firebaseio.com/studyGroups");
$scope.groups = $firebaseArray(groupsRef);
// This will give all the students. How do I get just the template's `group.members` to be each appropriate student?
var studentsRef = new Firebase("https://sof-data.firebaseio.com/students");
$scope.allStudents = $firebaseArray(studentsRef);
});
问题:
如何让 HTML 模板正确迭代 groups' members 并且我可以访问每个 student 的所有数据(例如他们的 firstName 和 grade)? (我假设它可能涉及控制器中更好的代码,也可能在 HTML 中)
PLNKR:http://plnkr.co/edit/uAn1Ky9v5NHHDFPs2c6S?p=preview (如果您知道如何在 plnkr 中注入 Firebase 和 angularFire,那么它应该可以工作,但是尽管包括了它们的 CDN,我仍然无法弄清楚...)
我能理解的最接近的解释是 Firebase 的 this page,他们建议使用 LINK_ID,但它没有被描述为他们的意思,特别是因为他们谈论不按 id 搜索。在我的例子中,link 是 group 的同义词,comment 是 student。
var commentsRef = new Firebase("https://awesome.firebaseio-demo.com/comments");
var linkRef = new Firebase("https://awesome.firebaseio-demo.com/links");
var linkCommentsRef = linkRef.child(LINK_ID).child("comments");
linkCommentsRef.on("child_added", function(snap) {
commentsRef.child(snap.key()).once("value", function() {
// Render the comment on the link page.
});
});
【问题讨论】:
标签: angularjs firebase angularfire