angularjs - How to get the data for my controller when http request in progress? -
i have following controller
1) introctrl
2) articlectrl
3) articleservice (service)
now sending http request introcrtl
.controller('introctrl', function($scope, articleservice) { articleservice.getarticles(); }); and ariclectrl
.controller('articlectrl', function($scope,$rootscope,articleservice) { $scope.articles = articleservice.fetcharticles(); }) and service is
.service('articleservice', function ($http, $q) { var articlelist = []; var getarticles = function() { $http({ url: "muylink,co,", data: { starlimit: 0, endlimit: 150,created_date: 0 }, method: 'post', withcredentials: true, }).success(function (data, status, headers, config) { articlelist.push(data); }).error(function (err) { console.log(err); }) }; var fetcharticles = function() { return articlelist[0]; } return { getarticles: getarticles, fetcharticles: fetcharticles }; }); which working fine. problem that
sometimes http request sending respone late , got nothing in
$scope.articles.
can implement watch here. how need implement $watch here. dont want implement promise. because want run http request behind scene.
thanks
it better if switch state based setup ui-router way can :
$stateprovider.state('mystate', { url: 'the/url/you/want', resolve:{ articleservice: 'articleservice' // dependency injecting here, articles: function (articleservice) { return articleservice.getarticles.$promise; } }, controller: 'introctrl' }) // controller can inject articles , resolved before controller loads fetched prior .controller('introctrl', function($scope, articles) { $scope.articles = articles; }); for more information take @
Comments
Post a Comment