gpt4 book ai didi

angularjs - 如何运行 Firebase 和 Angularjs Promises,例如 $loaded(),然后从依赖函数获取值

转载 作者:行者123 更新时间:2023-12-03 11:54:31 26 4
gpt4 key购买 nike

我正在构建 firebase Angular 应用程序:

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

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

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

最佳答案

关于angularjs - 如何运行 Firebase 和 Angularjs Promises,例如 $loaded(),然后从依赖函数获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25660382/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com