Javascript 如何在角度工厂中保留范围上的数据
我正在学习爱奥尼亚,我正在做一个辅导。一切都很好,直到我创建了一个访问Web服务的工厂 当我访问作用域中的变量时,它们没有被定义,或者只是像我初始化变量一样 这是密码 Controller.jsJavascript 如何在角度工厂中保留范围上的数据,javascript,angularjs,angular-services,Javascript,Angularjs,Angular Services,我正在学习爱奥尼亚,我正在做一个辅导。一切都很好,直到我创建了一个访问Web服务的工厂 当我访问作用域中的变量时,它们没有被定义,或者只是像我初始化变量一样 这是密码 Controller.js angular.module('songhop.controllers', ['ionic', 'songhop.services']) /*Controller for the discover page*/ .controller('DiscoverCtrl', function($sco
angular.module('songhop.controllers', ['ionic', 'songhop.services'])
/*Controller for the discover page*/
.controller('DiscoverCtrl', function($scope, $timeout, User, Recommendations) {
// get our first songs
Recommendations.getNextSongs()
.then(function(){
$scope.currentSong = Recommendations.queue[0];
console.log($scope.currentSong);
});
console.log(Recommendations.queue);
console.log($scope.currentSong);
// Fired when a song is favorited or skiped
$scope.sendFeedback = function (bool){
Recommendations.nextSong();
// First add to favorites if they favorited
if (bool) User.addSongToFavorites($scope.currentSong);
$scope.currentSong.rated = bool;
$scope.currentSong.hide = true;
$timeout(function() {
$scope.currentSong = Recommendations.queue[0];
}, 250);
};
})
/*
Controller for the favorites page
*/
.controller('FavoritesCtrl', function($scope, User) {
// get the list of our favorites from the user service
$scope.favorites = User.favorites;
$scope.removeSong = function(song, index) {
User.removeSongFromFavorites(song, index);
};
})
angular.module('songhop.services', []).factory('User', function() {
var o = {
favorites: []
}
o.addSongToFavorites = function(song){
// Make sure there is a song to add
if (!song) return false;
// Add to favorites array
o.favorites.unshift(song);
}
o.removeSongFromFavorites = function(song, index) {
// make sure there is a song to remove
if (!song) return false;
// remove to favorites array
o.favorites.splice(index, 1);
}
return o
})
.factory('Recommendations', function($http, SERVER) {
var p = {
queue: []
};
p.getNextSongs = function() {
return $http({
method: 'GET',
url: SERVER.url + '/recommendations'
}).success(function(data){
// merge data into the queue
p.queue = p.queue.concat(data);
});
};
p.nextSong = function() {
// pop the index 0 off
p.queue.shift();
// low on the queue? lets fill it up
if (p.queue.length <= 3) {
p.getNextSongs();
}
};
return p;
})
Service.js
angular.module('songhop.controllers', ['ionic', 'songhop.services'])
/*Controller for the discover page*/
.controller('DiscoverCtrl', function($scope, $timeout, User, Recommendations) {
// get our first songs
Recommendations.getNextSongs()
.then(function(){
$scope.currentSong = Recommendations.queue[0];
console.log($scope.currentSong);
});
console.log(Recommendations.queue);
console.log($scope.currentSong);
// Fired when a song is favorited or skiped
$scope.sendFeedback = function (bool){
Recommendations.nextSong();
// First add to favorites if they favorited
if (bool) User.addSongToFavorites($scope.currentSong);
$scope.currentSong.rated = bool;
$scope.currentSong.hide = true;
$timeout(function() {
$scope.currentSong = Recommendations.queue[0];
}, 250);
};
})
/*
Controller for the favorites page
*/
.controller('FavoritesCtrl', function($scope, User) {
// get the list of our favorites from the user service
$scope.favorites = User.favorites;
$scope.removeSong = function(song, index) {
User.removeSongFromFavorites(song, index);
};
})
angular.module('songhop.services', []).factory('User', function() {
var o = {
favorites: []
}
o.addSongToFavorites = function(song){
// Make sure there is a song to add
if (!song) return false;
// Add to favorites array
o.favorites.unshift(song);
}
o.removeSongFromFavorites = function(song, index) {
// make sure there is a song to remove
if (!song) return false;
// remove to favorites array
o.favorites.splice(index, 1);
}
return o
})
.factory('Recommendations', function($http, SERVER) {
var p = {
queue: []
};
p.getNextSongs = function() {
return $http({
method: 'GET',
url: SERVER.url + '/recommendations'
}).success(function(data){
// merge data into the queue
p.queue = p.queue.concat(data);
});
};
p.nextSong = function() {
// pop the index 0 off
p.queue.shift();
// low on the queue? lets fill it up
if (p.queue.length <= 3) {
p.getNextSongs();
}
};
return p;
})
因为$scope变量应该是全局变量,所以没有将$scope.currentSong变量设置为它应该设置的值,对吗?第二个和第三个控制台日志不会返回任何数据,因为它们在执行推荐返回的承诺之前执行了。getNextSongs()已解决 第一个显示数据,因为您已将其正确地放置在then块中,该块仅在承诺解析时执行。即,当方法建议.getNextSongs()完成时 如果您像下面这样更新代码,每个控制台将记录一些内容:
Recommendations.getNextSongs()
.then(function(){
$scope.currentSong = Recommendations.queue[0];
console.log($scope.currentSong);
console.log(Recommendations.queue);
console.log($scope.currentSong);
});
您确定在加载数据的
承诺完成之前,$scope.currentSong
的控制台.log
没有执行吗?感谢您的回复,它实际上是在承诺之前执行的。