2017-10-11 19 views
0

我想打電話給工廠的控制器http功能,它不會當我使用.then 我的代碼如下工作:的Http功能的控制器使用的時候不工作「然後」

模塊:

var brandlistmodule = angular.module('newApp「)

工廠:

brandlistmodule.factory('BrandListFactory',["$http","$routeParams",function($http,$routeParams){ 
    console.log(2) 
    return { 
     a: function(response){ 
      console.log('1'); 
      return $http.get("/restful_api/category_1/?format=json").then(function(response){ 
       console.log('3'); 
        return response.data; 
      }); 

     }, 
     b: function(response){ 
      return $http.get("/restful_api/category_refer_a/?format=json").then(function(response){ 
        return response.data; 
      }); 

     }, 
     c: function(response){ 
      return $http.get("/restful_api/brand_test_search/?format=json").then(function(response){ 
        result = response.data 
        return result; 
      }); 

     }, 

    } 

}]) 

控制器:

brandlistmodule.controller('brandlistCtrl', ['BrandListFactory','$scope','$rootScope','$http',function (BrandListFactory,$scope,$rootScope,$http) { 
     $scope.$on('$viewContentLoaded', function() { 
     $rootScope.category = function(){ 
     BrandListFactory.a.success(function(data){ 
      console.log('9'); 
      $rootScope.category =data 
     }); 

    }; 

在它只能顯示「2」控制檯,如果我改變控制器如下,它會正常工作

brandlistmodule.controller('brandlistCtrl', ['BrandListFactory','$scope',function (BrandListFactory,$scope) { 
    $scope.$on('$viewContentLoaded', function() { 
    BrandListFactory.BrandCategoryList() 

回答

1

你的問題是你打電話然後()在工廠裏面...所以你可以做的是:

1 - NO呼喚的話..但RETURN ALL YOUR $ HTTP請求到廠來電..像:

brandlistmodule.factory('BrandListFactory',["$http","$routeParams",function($http,$routeParams){ 
    console.log(2) 
    return { 
     a: function(response){ 
      console.log('1'); 
      return $http.get("/restful_api/category_1/?format=json"); 

     }, 
     b: function(response){ 
      return $http.get("/restful_api/category_refer_a/?format=json"); 

     }, 
     c: function(response){ 
      return $http.get("/restful_api/brand_test_search/?format=json"); 

     }, 

    } 

}]) 

2-使用$ q和自己做出asyncron鏈..像:

brandlistmodule.factory('BrandListFactory',["$http","$routeParams","$q",function($http,$routeParams,$q){ 
    console.log(2) 
    return { 
     a: function(response){ 
var deferred = $q.defer(); 
      console.log('1'); 
      return $http.get("/restful_api/category_1/?format=json").then(function(response){ 
       console.log('3'); 
       deferred.resolve(response.data); 
      }).catch(function(err){ 
deferred.reject(err); 
}); 

return deffered.promise; 

     } 
// and same for others 
    } 

}]) 
+0

我非常感謝您的患者解釋,我已經用您的幫助解決了這個問題。謝謝 –

+0

如果它對您有幫助的話..很高興!!! .. thnx –

0

我建議創造一個服務,這是負責所有如果您傳遞參數回調到HttpService.CallService你的HTTP調用像這樣

.service('HttpService', ['$rootScope', '$http', 'Ls', 'CommonService', 'DateService', function ($rootScope, $http, Ls, CommonService, DateService) { 
     return { 
      CallService: function (url, callback) {     
       $http.get(url) 
        .success(function (data, status) {        
         callback(data, status); 
        }).error(function (data, status) { 
         callback(data, status); 
        });     
      } 
     } 
    }); 

,那麼你可以調用successerror樂趣回調撥打http.get電話。

例如在控制器,你可以做

HttpService.CallService('/restful_api/category_refer_a/?format=json', function (data) { 
         console.log(data) 
         //do something with your data 

    }); 

因此,在您例如,你可以這樣做:

$scope.$on('$viewContentLoaded', function() { 
      HttpService.CallService('/restful_api/category_refer_a/?format=json', function (data) { 
          console.log('9'); 
       $rootScope.category =data 

     }); 
}); 

,或者你只是改變你的來電

b: function(callback){ 
      return $http.get("/restful_api/category_refer_a/?format=json").then(function(response){ 
        callback(response.data); 
      }); 

     }, 

BrandListFactory.b(function(data){ 
      console.log('9'); 
      $rootScope.category =data 
     }); 

希望這有助於你

+0

是的,你告訴我爲什麼它不能工作:在工廠中,如果你堅持「那麼」,你更願意使用「返回」而不是「那麼」,你應該使用$ q來解決可能的異步問題。 ? –

相關問題