javascript – 如何在Angular工厂中保留Scope上的数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 如何在Angular工厂中保留Scope上的数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2740字,纯文字阅读大概需要4分钟。
内容图文
![javascript – 如何在Angular工厂中保留Scope上的数据](/upload/InfoBanner/zyjiaocheng/773/9148b848b3794954a45d8f4b645ab7f0.jpg)
我正在学习Ionic,我正在做一个教程.一切都很好,直到我创建了一个访问Web服务的工厂.
当我访问范围中的变量时,它们没有被定义,或者它们就像我初始化变量一样.
这是代码
Controller.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);
};
})
Service.js
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;
})
在我测试的console.logs行中,我在第一行中获得了正确的数据.第二个是[],第三个是未定义的.
我不明白为什么
$scope.currentSong = Recommendations.queue[0];
是不是将$scope.currentSong变量设置为它应该是什么,因为$scope变量应该是全局的,对吧?
解决方法:
第二个和第三个控制台日志不会返回任何数据,因为它们在Recommendations.getNextSongs()返回的promise已解决之前正在执行.
第一个显示数据,因为您已将其正确放置在then块中,该块仅在promise解析时执行.即当Sugations.getNextSongs()方法完成时.
如果你更新了下面的代码,每个控制台都会记录一些东西:
Recommendations.getNextSongs()
.then(function(){
$scope.currentSong = Recommendations.queue[0];
console.log($scope.currentSong);
console.log(Recommendations.queue);
console.log($scope.currentSong);
});
内容总结
以上是互联网集市为您收集整理的javascript – 如何在Angular工厂中保留Scope上的数据全部内容,希望文章能够帮你解决javascript – 如何在Angular工厂中保留Scope上的数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。