0

I am pretty new to angularfire but I am trying to create an array that is a list of names from a firebase array. When I look at what lists returns it seems to be what I want, so I know the function is working correctly.

angular.module('myApp')
  .factory('QuestionType', function ($firebaseArray, $firebaseObject, Ref) {
    var types = $firebaseArray(Ref.child('questionTypes'));

    var questionType = {
      list: function(){
        var lists =[];
        types.$loaded().then(function(){
          for(var i=0; i<types.length; i++){
            lists.push(types[i].name);
          }
          return lists;
        });

      }
    };  
    return questionType;
  });

In my controller I call that factory but because it is asynchronous, when I try and use the array in my view, it comes back as undefined.

$scope.questionType = QuestionType.list();

I figured out that I need to use $loaded.then in my factory, but how do I do a similar thing in my controller so that I can properly use it in my view?

Thanks!

1 Answer 1

1

Return a new promise generated by .then(). Previously your list function didn't return anything.

angular.module('myApp')
  .factory('QuestionType', function ($firebaseArray, $firebaseObject, Ref) {
    var types = $firebaseArray(Ref.child('questionTypes'));

    var questionType = {
      list: function(){

        return types.$loaded().then(function(){
          var lists =[];
          for(var i=0; i<types.length; i++){
            lists.push(types[i].name);
          }
          return lists;
        });

      }
    };  
    return questionType;
    });

--

QuestionType.list().then(function(list){
  $scope.questionType = list;
});
Sign up to request clarification or add additional context in comments.

Comments

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.