带角度路由器的firebase永远不会到达控制器

时间:2015-04-07 17:01:59

标签: angularjs firebase angular-ui-router angularfire

我想确保在加载应用之前从firebase获取数据。我正在使用此代码,但它无效:程序永远不会到达代码deferred.resolve();appCtrl无法运行... 有什么想法吗?

appCtrl:

.controller('AppCtrl', function($scope, $ionicModal, $timeout,$firebaseAuth,$state,myWordsListServ) {    
  $scope.myWordsList = myWordsListServ.contacts;
  console.log("myWordsList" , $scope.myWordsList);
})

myWordsListServ:

.factory('myWordsListServ', function($firebase, $q) {
  return {
    contacts: null,
    promiseToHaveMyLists: function() {
      var deferred = $q.defer();

       var firebaseMainRef = new Firebase("https://myApp.firebaseio.com");
       var authData = firebaseMainRef.getAuth();
       if(authData){
         this.contacts = $firebase(new Firebase("https://myApp.firebaseio.com/users/" + authData.uid));
         this.contacts.$on('value', function(loadedData) {
           deferred.resolve();
         });
       }
       else{
         deferred.resolve();
       }
      return deferred.promise;
    }
  };
})

我app.js的一部分:

.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider

  .state('app', {
    url: "/app",
    abstract: true,
    templateUrl: "templates/menu.html",
    controller: 'AppCtrl',
    resolve : {
        myWordsList : function(myWordsListServ) {
            return myWordsListServ.promiseToHaveMyLists();
        }
    },
  })

1 个答案:

答案 0 :(得分:1)

您似乎使用的是1.0版之前的AngularFire:

this.contacts = $firebase(new Firebase("https://myApp.firebaseio.com/users/" + authData.uid));
this.contacts.$on('value', function(loadedData) {
  deferred.resolve();
});

$firebase类是一个中间对象,(如果我没记错的话)永远不会找到事件。您可能希望做的是:

this.contacts = $firebase(new Firebase(...)).$asObject();

这会返回$firebaseObject,其中执行触发事件。

您可能希望升级到AngularFire 1.0,在这种情况下,您可以获得相同的结果:

this.contacts = $firebaseObject(new Firebase(...));