我在将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
为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;