1

My HTML is

  <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>

In my controller, I have

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();
});

It somehow seems unnecessary to have to iterate over the returned object to achieve what I am trying to do. Is there a better / more native way of doing this? I've looked into the supposed three way data binding on the Firebase doc site but that doesn't show how to do it if you are filtering objects.

1 Answer 1

3

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Can I use this with filtering as well?
Yep. Just make a query from the ref and pass it into the $firebaseArray.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.