I want to implement a directive, which function is mainly display the banner image. The banner image url is got by making a get request to server. So I write a service to get the image url as below:
service('NavBannerService', ['urls', '$http', '$cookies', function(urls, $http, $cookies){
var banner_pic_url = '';
var refresh_banner = function(){
$http.get(urls.api + '/news/banner_url').success(function(data){
//console.log(data);
banner_pic_url = data.banner;
});
};
refresh_banner();
return {
nav_pic: function(){
return banner_pic_url;
}
};
}])
and my directive is:
directive('navBanner', ['urls', 'NavBannerService', function(urls, nbs){
return {
restrict: 'E',
scope: {
},
controller: function($scope){
$scope.banner_pic_url = nbs.nav_pic();
},
templateUrl: urls.part + '/directive_html/nav_banner.html'
};
}])
But sometimes the $scope.banner_pic_url in the directive controllers will be '', because the nbs.nav_pic() called before the http request get a response.
How can I solve this problem? Or is there any better solutions. ( Using service to get the banner_url is because the directive will be in many pages, I don't want to call the page_url each time getting in a new page)
Thank you.