我正在尝试构建一个简单的角度应用程序,该应用程序可以从Instagram中获取数据。用户在索引页上输入hashtag,然后显示到另一个页面,其中显示带有该hashtag的帖子。角度服务:查看更改时丢失的值
我试过传递用作服务中变量的hashtag,但是当视图改变时,该值被覆盖。 (我可以在设置它之后检查它的值,并且它已设置,但只要页面更改,我将失去该值)。
这里是我的服务:
var instagramApp = angular.module('instagramApp')
.factory('feedData', function($rootScope) {
var config = {};
return {
setHashtag: function (x) {
config.hashtag = x;
},
getHashtag: function() {
return config.hashtag;
}
}
});
和我的两个控制器:
集包括hashtag(在/index.html视图):
instagramApp.controller('indexController', ['$scope', 'feedData', '$window',
function($scope, feedData, $window){
$scope.generate = function(){
feedData.setHashtag('marcheet');
console.log(feedData.getHashtag());
$window.location.href = '/results.html';
};
}]);
获取包括hashtag(在/results.html查看):
instagramApp.controller('instagramController', ['$scope', 'Instagram', '$http', 'feedData',
function($scope, Instagram, $http, feedData){
feedUrl = '/feed/?hashtag=' + feedData.getHashtag() +'&count=20';
console.log(feedUrl);
createStoryJS({
type: 'timeline',
width: '800',
height: '600',
source: feedUrl,
embed_id: 'my-timeline'
});
}
]);
$ window.location.href = '/results.html' 的配置方法;这条线就在这里,你刚刚离开了你的角度应用程序,所以这就是为什么它不知道它。 –