2016-04-16 24 views
0

を使用してサービスを注入することはできません私はES6の構文を使用してコントローラにサービスをインポートしようとしているが、私は、注射の問題にはES6構文

CategoriesService.js

export default class CategoriesService { 
    constructor() { 
     this.getCategories = function ($q) { 
      var deferred = $q.defer(); 
      deferred.resolve([ 
       { 
        id: '1', 
        name: 'Category One', 
        slug: 'category_one', 
        profile: { 
         id: '1', 
         name: 'Thomas Wayne', 
         location: '1007 Mountain Drive, Gotham', 
         description: 'Dr. Thomas Wayne, one of the most respected patrons in all of Gotham City', 
         images: ['...', '...'], 
         featuredImage: '...' 
        } 
       }, 
       { 
        id: '2', 
        name: 'Category Two', 
        slug: 'category_two', 
        profile: { 
         id: '2', 
         name: 'Martha Kane', 
         location: '1007 Mountain Drive, Gotham', 
         description: 'Martha Wayne (née Kane) was born into the Kane family, who were so rich that they allegedly "owned the half of Gotham that the Waynes don\'t"', 
         images: ['...', '...'], 
         featuredImage: '...' 
        } 
       } 
      ]); 
      return deferred.promise; 
     } 
    } 

} 


CategoriesService.$inject = []; 

を取得していますCategoriesController.js

import CategoriesService from './CategoriesService'; 

export default class CategoriesController { 
    constructor(CategoriesService) { 
     CategoriesService.getCategories().then(getCategoriesSuccessFn, getCategoriesFailFn); 

     function getCategoriesSuccessFn(data) { 
      this.categories = data; 
     } 

     function getCategoriesFailFn() { 
      console.error('Something went wrong'); 
     } 
    } 
} 
CategoriesController.$inject = ['CategoriesService']; 

エラー

angular.js:13424 Error: [$injector:unpr] Unknown provider: CategoriesServiceProvider <- CategoriesService 
http://errors.angularjs.org/1.5.3/$injector/unpr?p0=CategoriesServiceProvider%20%3C-%20CategoriesService 
    at angular.js:68 
    at angular.js:4418 
    at Object.getService [as get] (angular.js:4571) 
    at angular.js:4423 
    at getService (angular.js:4571) 
    at injectionArgs (angular.js:4595) 
    at Object.invoke (angular.js:4617) 
    at $controllerInit (angular.js:10027) 
    at nodeLinkFn (angular.js:8965) 
    at angular.js:9362 

私はここで何が不足していると思いますか?

答えて

2

ES6のインポート/エクスポートでも、サービスを登録する必要があります。

App.js

import CategoriesService from './CategoriesService'; 
import CategoriesController from './CategoriesController'; 

angular.module('app', []) 
    .service('CategoriesService', CategoriesService) 
    .controller('CategoriesController', CategoriesController); 
+1

私はコントローラと同じ問題を持って、同じアプローチを使用して、それを修正しました。ありがとう! – chachan

関連する問題