2016-06-10 71 views
0

我正在旋转我的轮子。我正试图向我的控制器提供此服务的一个实例。当我运行它,我得到角度服务投掷“未知提供者”

JavaScript runtime error: [$injector:unpr] Unknown provider: app.services.GithubServiceProvider <- app.services.GithubService <- app.githubViewer.UserController

这里有件:

  1. 服务本身

    module app.services { 
    'use strict'; 
    
    export interface IGithubService { 
        getUser(username: string): ng.IPromise<any>; 
    } 
    
    export class GithubService implements IGithubService { 
    
        githubUrl: string = 'https://api.github.com/'; 
    
        static $inject = ['$http']; 
        constructor(private $http: ng.IHttpService) { 
    
        } 
        getUser(username: string): angular.IPromise<any> { 
         return this.$http.get(this.githubUrl + "users/" + username) 
          .then(response => response.data); 
        } 
    } 
    
    angular 
        .module('app.services') 
        .service('app.services.GithubService', GithubService); 
    

    }

  2. 的控制器

    module app.githubViewer { 
    'use strict'; 
    
    interface IUserControllerScope { //scope definitions 
    
    } 
    
    class UserController implements IUserControllerScope { 
    
        static $inject = ['app.services.GithubService', '$route']; 
        constructor(githubService: app.services.IGithubService, $route: ng.route.IRouteService) { 
        //... 
        } 
    
    } 
    
    angular 
        .module('app.githubViewer') 
        .controller('app.githubViewer.UserController', UserController); 
    

    }

更新,我看着的常见问题this list在这里,它看起来像它的一切都是为了。代码如下:

这里是我的主模块声明:

((): void => { 
    'use strict'; 

    angular 
     .module('app', [ 
      'app.core', 
      'app.services', 
      /* 
      *Feature Modules 
      */ 
      'app.githubViewer' 
     ]); 
})(); 

这里是服务模块声明:

((): void => { 
    'use strict'; 

    angular 
     .module('app.services', []); 
})(); 

我有三重检查,以确保脚本添加到页。

请让我知道如果有什么我应该在这里发布。我是Angular和TypeScript的初学者。

+2

能否请您提供您的'app.githubViewer'模块声明?看看@emed答案,并检查是否是你的问题。 –

+0

埃梅德是正确的。我不确定我是否明白为什么,但是修正了它。我非常严格地遵循一个示例,它没有'app.services'作为app.githubViewer的依赖关系,因为它已经是“app”的依赖关系。我会读更多关于这个。 – jlembke

+2

每个模块都有自己的依赖关系。 'app.'前缀只是为了“组织”模块的层次结构,但它只是一个名称,angular并不关心模块名称。如果你在你的“main”模块('app')中放置一个依赖项,它并不重要。如果您需要在另一个模块中使用依赖项,则需要将其声明为该模块的依赖项。如果您有任何疑问,请免费索取:) –

回答

1

您必须声明app.services作为一个依赖于app.githubViewer

angular 
    .module('app.githubViewer', ['app.services']) 
相关问题