2016-08-02 56 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']) 
相关问题