2016-08-05 45 views
0

我有一個要求在使用angular js提供程序加載頁面之前獲取數據,我沒有實現它,請任何人幫助我。如何在angularjs提供者加載頁面之前獲取數據

這是我的代碼,請通過它

hiregridApp.provider('organizationService', function() { 
    return { 
    getOrganization: ['$http', '$location', 
     function($http, $location) { 
     $http({ 
      method: 'GET', 
      url: http: //dev.api.hiregrid.io/api/customer/token/hiregrid', 
     }).success(function(data) { 
      $log.log(data); 
     }).error(function(error, status) { 
      $routeParams.code = status; 
      $location.path('/error/' + $routeParams.code); 
     }); 
     } 
    ] 
    }, this.$get: ['$http', '$location', 
    function($http, $location) { 
     var obj = ''; 
     alert("hai"); 
     obj.getOrganization = function() { 
     $http({ 
      method: 'GET', 
      url: 'http://dev.api.hiregrid.io/csbuilder- api/api/csbuilder/hiregrid', 
     }).success(function(data) { 
      $log.log(data); 
     }).error(function(error, status) { 
      $routeParams.code = status; 
      $location.path('/error/' + $routeParams.code); 
     }); 
     return obj; 
     } 
    } 
    ]; 
}); 
hiregridApp.config(function(organizationServiceProvider) { 
    console.log(organizationServiceProvider); 
    organizationServiceProvider.getOrganization("http://dev.api.hiregrid.io"); 
}); 
+0

這個功能已經在'ngRoute'或第三方路由器'UI-router'的'resolve'參數提供。如果沒有對異步方法的深入理解,試圖手動解決它將會非常困難。 – Claies

回答

0

如果要加載網頁加載數據之前,使用resolve你的路由器裏面。爲此你不需要提供者。 關於resolve你可以在這裏閱讀angular router

0

你可以在路由,以便當過您導航到一個頁面,將解決你的數據,然後導航解決您的數據。 請注意,如果服務器需要很長時間來響應,所以它不會導航u直到它解決了您的承諾。

你可以在你有你的路由器配置的配置中使用你的提供者。

angular.module ('app',[]).config (function(yourProvider){}) 
0
var config = { headers : {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8;'}}; 
    App.provider('organizationService', ['$http', 
    function($http) { 
     return { 
      getOrganization: function(){ 
       $http.get("http: //dev.api.hiregrid.io/api/customer/token/hiregrid",config) 
       .success(function(data) { 
     $log.log(data); 
    }).error(function(error, status) { 
     $routeParams.code = status; 
     $location.path('/error/' + $routeParams.code); 
    }); 
      } 
     } 
    } 
]); 
+0

App.controller('JobPositionCtrl',函數($ rootScope,$ scope,organizationService){} –

相關問題