我的 HTML 是
<tbody>
<tr ng-repeat="question in questions">
<td>
<a ui-sref="englishDetail({id: question.id})">
{{ question.id }}
</a>
</td>
<td>{{ question.instruction }}</td>
<td>{{ question.questionText }}</td>
<td>{{ question.level }}</td>
<td>
<a ui-sref="passageDetail({id: question.passageId})">
{{ question.passageId }}
</a>
</td>
</tr>
</tbody>
在我的 Controller 中,我有
var fbase;
$scope.questions = [];
fbase = new Firebase("https://my.firebaseio.com");
fbase.child("questions").orderByChild("subject").equalTo("english").once("value", function(questionsSnapshot) {
$scope.questions = [];
questionsSnapshot.forEach(function(questionSnapshot) {
$scope.questions.push({
id: questionSnapshot.key(),
instruction: questionSnapshot.val().instruction,
questionText: questionSnapshot.val().questionText,
level: questionSnapshot.val().level,
passageId: questionSnapshot.val().passageId
});
});
return $scope.$apply();
});
似乎没有必要迭代返回的对象来实现我正在尝试做的事情。有没有更好/更本土的方式来做到这一点?我已经在 Firebase 文档站点上研究了假设的三向数据绑定(bind)
,但是如果您要过滤对象,那并没有说明如何做到这一点。
最佳答案
Use AngularFire for synchronized collections.
angular.module('app', ['firebase'])
.constant('FirebaseUrl', '<my-firebase-app>')
.service('rootRef', ['FirebaseUrl', Firebase])
.controller('MyCtrl', MyController);
function MyController($scope, rootRef, $firebaseArray) {
var ref = rootRef.child('questions');
// handles $digest loop and synchronizing to an array
$scope.questions = $firebaseArray(ref);
}
关于javascript - 如何将返回的 Firebase 元素数组绑定(bind)到我的 AngularJS View (通过 Controller )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34399744/