angularjs - Return data from Service, not promise -
i'm looking have bit of cleaner controller not muddy promise api stuff.
here's example of i'm talking about:
some service
... .service('someservice', function someservice($http) { this.getallitems = function() { return $http.get('api/items') .success(function(data) { return data; }); controller
... someservice.getallitems().then(function(response) { $scope.items = response.data; }); ... i'm trying avoid function call on promise , assign items directly so:
$scope.items = someservice.getallitems(); is possible? i've tried calling within service, , still ends returning promise object once it's resolved in controller, i'm doing wrong. thanks!
you can use $resource class it. from documentation
it important realize invoking $resource object method returns empty reference (object or array depending on isarray). once data returned server existing reference populated actual data. useful trick since resource assigned model rendered view. having empty object results in no rendering, once data arrives server object populated data , view automatically re-renders showing new data. means in cases 1 never has write callback function action methods.
Comments
Post a Comment