2016-06-10 76 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']) 
相關問題