我想确保在加载应用之前从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();
}
},
})
答案 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(...));