使用路由解析允许promise在控制器逻辑执行之前解析,因此我想在ui-router配置中解析我的promise。我有3个文件: app.js , main.controller.js 和 products.service.js
app.js
angular.module('app',[
'Products',
'ui.router'
])
.config(config);
config.$inject = ['$stateProvider','$urlRouterProvider'];
function config($stateProvider,$urlRouterProvider){
// For any unmatched url, redirect to /state1
$urlRouterProvider.otherwise("/products");
// Now set up the states
$stateProvider
.state('productsState', {
url: "/products",
templateUrl: "views/products.html",
controller: "MainController as Main",
resolve: MainController.resolve
});
}
main.controller.js
angular.module('app')
.controller('MainController', MainController);
MainController.resolve = {
_products: ['ProductsService', function (ProductsService) {
return ProductsService.getProducts();
}]
};
MainController.$inject = ['_products'];
function MainController(_products){
var vm = this;
vm.test = 'Angular 1.4.8';
vm.products = _products;
}
products.service.js
angular.module('Products',[])
.factory('ProductsService', ProductsService);
ProductsService.$inject = ['$http'];
function ProductsService($http){
return {
getProducts:getProducts
};
function getProducts(){
return $http.get('some-url').then(function(response){
return response.data;
});
}
}
未知提供商:_productsProvider< - _products< - MainController