2016-08-02 48 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']) 
相關問題