javascript - 如何将返回的 Firebase 元素数组绑定(bind)到我的 AngularJS View (通过 Controller )?

标签 javascript angularjs firebase

我的 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/

相关文章:

javascript - Durandal View 偶尔无法正确加载,并且在缩小时总是无法正确加载

angularjs - 如何在 Angular 混合应用程序中的组件之间进行通信?

angularjs - 使用 ng-view 时出现scrollTo错误

javascript - 获取到 https :www. google.com 的请求失败(显然)与 CORS

javascript - 使用 Lat Long 的 Bubble Highmap

Javascript Electron 从列表中删除/忽略某个项目

javascript - Angular 错误 [$injector :modulerr] when loading module

javascript - 在 Firebase Cloud Functions 中创建 PDF

node.js - Node 邮件程序错误

android - 新的 'Cloud Firestore' 是否可以查询基于位置的距离?