如何运行Firebase和Anguarjs Promises,如$loaded(),然后从依赖函数中获取值

How to run Firebase and Angualrjs Promises such as $loaded() and then to get values from dependant functions

本文关键字:然后 依赖 loaded 函数 获取 Promises 何运行 运行 Firebase Anguarjs      更新时间:2023-09-26

我正在构建火球角应用程序:

var myApp = angular.module('appStart',[]);
myApp.controller('MyController', function($scope,$firebase,,MyFactory){
//this is returning undefined as this point
//How can i pass angualrjs promises so that it returns values once factory finish runnig all operations
    var returnData = MyFactory.getUsersProfile(userId);
});
myApp.factory('MyFactory', ['$firebase', function($firebase){
        var factory = {};
        factory.getUsersProfile = function(userId){
        var firebaseRef = new Firebase("https://xxx.firebaseio.com/userprofile").child(userId);
                //retrive the data
                firebaseRef.on('value', function (snapshot) {
                    console.log(snapshot.val());
                    var data = snapshot.val();
                    if (data){
 //Here I want to call another function and get and return values once called function finish running
                        var data = factory.getUsersBetsEventsDetails(data); 
                        return data;
                    }else{
                        console.log('return nothing');
                    }
                }, function (errorObject) {
                    console.log('The read failed: ' + errorObject.code);
                });
            }
            factory.getUsersBetsEventsDetails = function(usersProfileData){
                var firebaseBetsRef = new Firebase("https://xxx.firebaseio.com/e");
                var userE = [];
                angular.forEach(usersProfileData, function(usersProfileData){
                    console.log('getUsersBets',usersProfileData);
                    var firebaseEData = firebaseBetsRef.child(usersProfileData.e_id);
                    //retrive the data 
                    firebaseE.on('value', function (snapshot) {
                        console.log(snapshot.val());
                        userE.push(snapshot.val());
                        console.log('userE',userE);
                    }, function (errorObject) {
                        console.log('The read failed: ' + errorObject.code);
                    });
                    //this is the values controller should get 
                    retuen userE;
                }); 
            }
            return factory;
        }]);

正如我在评论中所写的那样,我希望在完成运行后运行依赖代码,但不确定如何实现这一点?

你在这段代码中有什么不确定的吗?请问。感谢

我最终使用了回调:如何在firebase中获得同步数据?和http://recurial.com/programming/understanding-callback-functions-in-javascript/