Processing $http response in service

后端 未结 12 1828
半阙折子戏
半阙折子戏 2020-11-22 03:36

I recently posted a detailed description of the issue I am facing here at SO. As I couldn\'t send an actual $http request, I used timeout to simulate asynchrono

12条回答
  •  温柔的废话
    2020-11-22 04:32

    I've read http://markdalgleish.com/2013/06/using-promises-in-angularjs-views/ [AngularJS allows us to streamline our controller logic by placing a promise directly on the scope, rather than manually handing the resolved value in a success callback.]

    so simply and handy :)

    var app = angular.module('myApp', []);
                app.factory('Data', function($http,$q) {
                    return {
                        getData : function(){
                            var deferred = $q.defer();
                            var promise = $http.get('./largeLoad').success(function (response) {
                                deferred.resolve(response);
                            });
                            // Return the promise to the controller
                            return deferred.promise; 
                        }
                    }
                });
                app.controller('FetchCtrl',function($scope,Data){
                    $scope.items = Data.getData();
                });
    

    Hope this help

提交回复
热议问题