2016-08-05 42 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){} –

相关问题