javascript - AngularJS return value from factory to controller out of Promise -
i have http request setup promise in .factory , send result controller. stuck getting data out of promise , somewhere can use it.
.controller
.controller('mainctrl', function ($scope, main, user) { $scope.feedresult = function () { var feeds = []; return main.feed(key).success(function (data, status, headers, config) { (var = 0; < data.length; += 1) { feeds.push(data[i]); } return feeds; }).error(function (data, status, headers, config) { console.log("this error server"); console.log(data); return data; }); }(); })
and .factory
feed: function (token) { return $http({ url: 'http://myserver.com/feed', method: 'get', data: { auth_code: token, per_page: 10 } }).success(function (data, status, headers, config) { (var = 0; < data.length; += 1) { feeds.push(data[i]); } return feeds; }).error(function (data, status, headers, config) { console.log("this error server"); console.log(data); return data; }); },
mo matter how seem try putting things scope seem end promise object. can me out?
the success or error method return promise (it wraps return value new promise). shoudl set value on scope within success/error callback:
.controller('mainctrl', function ($scope, main, user) { $scope.feedresult = []; main.feed(key).success(function (data, status, headers, config) { (var = 0; < data.length; += 1) { $scope.feedresult.push(data[i]); } }).error(function (data, status, headers, config) { console.log("this error server"); console.log(data); $scope.feedresult = data; }); })
Comments
Post a Comment