如何將服務注入到我的路由器中,以便其結果(json)將在整個應用程序中可用?Angular js。在路由器中注入服務
路由器:
export default ['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider, MainController, ConfigService) {
$stateProvider.state('home', {
url: '/',
controller: 'MainController',
resolve: {
/* @ngInject */
someResolve: (ConfigService) =>
ConfigService.config()
}
});
$urlRouterProvider.otherwise('/');
}];
服務:
export default function ConfigService() {
return ['$http', function($http) {
this.config = function() {
return $http({
url: '../json/config.json',
method: 'GET'
}).then(function(result) {
console.log(result);
});
};
}];
}
Maincontroller
export default function MainController() {
return ['$scope','$location','ConfigService','$window', function($scope, $location, ConfigService, $window) {
ConfigService.config();
}];
}
如果你要存儲此JSON做?例如在$ rootScope中,一個全局變量,一個帶有getter等服務的局部變量? –
我希望它被存儲,以便整個應用程序可以從一開始就訪問它。那怎麼做對我來說並不重要。 – user2952238