2016-08-02 4 views
0

私はまだ把握し始めている概念であるIIFEを使用するプロジェクトに取り組んでいます。ここコントローラにサービスを注入すると、角度が不明なプロバイダエラーが発生する

Unknown provider: StudentsServiceProvider <- StudentsService <- StudentsController 

は、問題のコントローラです:私のサービスは、私はそれが定義されているが、私は私のコントローラに注入しようとすると、私はこのエラーを取得することを決定するために、いくつかのジャスミンを使用しています、罰金のようです

(function() { 
    'use strict'; 

    angular 
     .module('ngInterview.students') 
     .controller('StudentsController', StudentsController); 

    StudentsController.$inject = ['StudentsService']; 
    function StudentsController(StudentsService) { 

     /** 
     * Model 
     */ 

     var vm = this; 

     /** 
     * Initialization 
     */ 

     activate(); 

     /** 
     * Implementations 
     */ 

     function activate() { 
      // Initialization code goes here 
      vm.students = StudentsService.getStudents(); 
     } 
    } 
})(); 

そして、ここのサービスで、念のために私は何とかそこにめちゃめちゃ:

(function() { 
    'use strict'; 

    angular 
     .module('ngInterview.api.students') 
     .service('StudentsService', StudentsService); 

    StudentsService.$inject = ['$http']; 
    function StudentsService($http) { 

     /** 
     * Exposed functions 
     */ 

     this.getName = getName; // This function serves no purpose. It's just here as an example. 

     this.getStudents = function() { 
      return $http({ 
       url: "CUSTOM_URL_HERE", 
       method: "GET" 
      }).then(function successCallback(res) { 
       return res; 
      }, function errorCallback(res) { 
       return this.getStudents(); 
      }); 
     } 

     /** 
     * Implementations 
     */ 

     function getName() { 
      return 'studentsService'; 
     } 
    } 
})(); 

上記のファイルのすべてがindex.htmlの中に含まれています。 StudentsServiceへの参照を取り除くと、エラーは発生せず、すべてのファイルが正しくインスタンス化されます。

答えて

1

サービスStudentsServiceは別のmoduleであるので、あなたは以下のように、メインmodule'ngInterview.api.students'moduleを注入する必要があります。

angular 
    .module('ngInterview.students', ['ngInterview.api.students']) 
関連する問題