Angular JS - 无法在Controller

时间:2016-07-23 16:11:06

标签: javascript angularjs angular-ui-router ecmascript-6 angularjs-components

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

我已使用$qgetGarage(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;

0 个答案:

没有答案