Javascript 更新的范围变量未显示在视图中。承诺不起作用
我试图从Firebase数据库中检索数据,并从Firebase存储中检索相应的图像。问题是,我的视图不想用数据更新自己 如果我试着简单地从数据库中获取数据,它就可以完美地工作。一旦我添加了获取图片的功能(这需要稍长的时间),我的视图就会立即查看scope变量,而不会等待$scope.friendsinfo更新。我认为我的承诺有问题,应该使用$q,但我不知道具体如何。谁能告诉我最好的办法是什么?非常感谢Javascript 更新的范围变量未显示在视图中。承诺不起作用,javascript,angularjs,firebase,firebase-realtime-database,angularfire,Javascript,Angularjs,Firebase,Firebase Realtime Database,Angularfire,我试图从Firebase数据库中检索数据,并从Firebase存储中检索相应的图像。问题是,我的视图不想用数据更新自己 如果我试着简单地从数据库中获取数据,它就可以完美地工作。一旦我添加了获取图片的功能(这需要稍长的时间),我的视图就会立即查看scope变量,而不会等待$scope.friendsinfo更新。我认为我的承诺有问题,应该使用$q,但我不知道具体如何。谁能告诉我最好的办法是什么?非常感谢 var friendsRef = firebase.database().ref('frien
var friendsRef = firebase.database().ref('friendships/' + firebase.auth().currentUser.uid);
$scope.friends = $firebaseArray(friendsRef);
$scope.friendsinfo = [];
$scope.$watch('friends', function() {
var newfriends = $scope.friends;
var newfriendsinfo = [];
for(var i = 0; i < newfriends.length; i++){
var ref = firebase.database().ref('users/' + newfriends[i].$id);
var profilePicRef = firebase.storage().ref("profilepictures/" + newfriends[i].$id + "/profilepicture");
var picPromise = fetchPicture(profilePicRef);
var newfriendid = newfriends[i].$id;
var newfriendagreed = newfriends[i].agreed;
picPromise.then(function(data){
ref.once('value', function(snapshot){
newfriendsinfo.push({
id: newfriendid,
name: snapshot.val().name,
email: snapshot.val().email,
agreed: newfriendagreed,
profilepicture: data //This is the functionality that causes my view to not display the updated $scope.friendsinfo because it takes too long.
});
});
});
}
$scope.friendsinfo = newfriendsinfo;
alert($scope.friendsinfo.length);
}, true);
function fetchPicture(ref){
return ref.getDownloadURL().then(function(url) {
return url;
}).catch(function(error) {
alert("error");
});
}
var-friendsRef=firebase.database().ref('friendships/'+firebase.auth().currentUser.uid);
$scope.friends=$firebaseArray(friendsRef);
$scope.friendsinfo=[];
$scope.$watch('friends',function(){
var newfriends=$scope.friends;
var newfriendsinfo=[];
for(var i=0;i
我没有正确地获取您的代码,但发布了代码,您可以使用这些代码来指导如何使用承诺和解决方法:
function asyncGreet(name) {
var deferred = $q.defer();
setTimeout(function() {
deferred.notify('About to greet ' + name + '.');
if (okToGreet(name)) {
deferred.resolve('Hello, ' + name + '!');
} else {
deferred.reject('Greeting ' + name + ' is not allowed.');
}
}, 1000);
return deferred.promise;
}
var promise = asyncGreet('Robin Hood');
promise.then(function(greeting) {
alert('Success: ' + greeting);
}, function(reason) {
alert('Failed: ' + reason);
}, function(update) {
alert('Got notification: ' + update);
});
如果有人需要解决方案,就在这里。事实证明,问题主要是由等待for循环完成引起的,术语中的每个项都等待另一个函数完成。我就是这样解决的。这可能不是最佳的,但现在会做到:)
var-friendsRef=firebase.database().ref('friendships/'+firebase.auth().currentUser.uid);
$scope.friends=$firebaseArray(friendsRef);
$scope.friendsinfo=[];
$scope.$watch('friends',function(){
var newfriends=$scope.friends;
AsyncUpdateFriendInfo(newfriends).then(函数(newlist){
$scope.friendsinfo=newlist;
});
},对);
函数获取图片(ref){
return ref.getDownloadURL().then(函数(url){
返回url;
}).catch(函数(错误){
警报(“错误”);
});
}
函数AsyncUpdateFriendInfo(newfriends){
var deferred=$q.deferred();
var newfriendsinfo=[];
for(var i=0;i
var friendsRef = firebase.database().ref('friendships/' + firebase.auth().currentUser.uid);
$scope.friends = $firebaseArray(friendsRef);
$scope.friendsinfo = [];
$scope.$watch('friends', function() {
var newfriends = $scope.friends;
asyncUpdateFriendsInfo(newfriends).then(function(newlist){
$scope.friendsinfo = newlist;
});
}, true);
function fetchPicture(ref){
return ref.getDownloadURL().then(function(url) {
return url;
}).catch(function(error) {
alert("error");
});
}
function asyncUpdateFriendsInfo(newfriends){
var deferred = $q.defer();
var newfriendsinfo = [];
for(var i = 0; i < newfriends.length; i++){
var ref = firebase.database().ref('users/' + newfriends[i].$id);
var profilePicRef = firebase.storage().ref("profilepictures/" + newfriends[i].$id + "/profilepicture");
var picPromise = fetchPicture(profilePicRef);
var newfriendid = newfriends[i].$id;
var newfriendagreed = newfriends[i].agreed;
picPromise.then(function(data){
ref.once('value', function(snapshot){
newfriendsinfo.push({
id: newfriendid,
name: snapshot.val().name,
email: snapshot.val().email,
agreed: newfriendagreed,
profilepicture: data
});
}).then(function(){
if (newfriendsinfo.length == newfriends.length){
deferred.resolve(newfriendsinfo);
}
});
});
}
return deferred.promise;
}