我在将resolved
对象注入controller
. 我正在使用基于 AngularJS 1.5 组件的 + ES6 语法
继承人的routes.js
文件:
我想得到resolved object
garage
它并将其注入我的,但当我注入它之后GarageViewController
,不知何故garage
似乎是未定义的。
我是错过了一些过程还是只是对某些事情感到困惑,我不知道我做错了什么。
请帮助,建议非常感谢。
const MainRoutes = ($stateProvider, $locationProvider, $urlRouterProvider) => {
"ngInject";
$locationProvider.html5Mode(true).hashPrefix('!');
$urlRouterProvider.otherwise('/');
// Define the routes
$stateProvider
.state('dashboard.garage-view', {
url: '/dashboard/garage/:id',
template: '<garage-view></garage-view>',
views: {
'': { template: '<garage-view></garage-view>' },
'content@dashboard.garage-view': {
templateUrl: 'src/app/templates/garage.view.template.html'
},
resolve: {
garage: (GarageService,$stateParams) => {
return GarageService.getGarage($stateParams.id)
}
}
});
export default MainRoutes;
这是garage.service.js
:
我曾经$q
创建过promise函数getGarage(id)
用于routes.js
class GarageService {
constructor($http,$location,CONFIG_CONSTANTS,$q, AuthService) {
this.API_URL = CONFIG_CONSTANTS.API_URL;
this.$http = $http;
this.$location = $location;
this.$q = $q;
this.api_token = AuthService.api_token;
}
getGarage(id) {
const deferred = this.$q.defer();
this.$http.get(`${this.API_URL}/garage/${id}?api_token=${this.api_token}`)
.success(response => deferred.resolve(response))
.error(error => deferred.reject(error));
return deferred.promise;
}
}
GarageService.$inject = [
'$http',
'$location',
'CONFIG_CONSTANTS',
'$q',
'AuthService'
];
export default GarageService;
这是GarageViewController.js
class GarageViewController {
constructor(garage) {
console.log(garage)
}
}
GarageViewController.$inject = ['garage'];
export default GarageViewController;