繁体   English   中英

角服务抛出“未知提供者”

[英]Angular Service Throwing “Unknown Provider”

我正在为此而努力。 我正在尝试向控制器提供此服务的实例。 当我运行它时,我得到

JavaScript运行时错误:[$ injector:unpr]未知提供程序: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); 

    }

更新,我在这里查看了常见问题列表 ,看起来一切井井有条。 代码如下:

这是我的主要模块声明:

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

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

这是服务模块声明:

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

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

我已进行了三次检查,以确保将脚本添加到页面中。

请让我知道是否还有其他我应该在这里发布的信息。 我是Angular和TypeScript的初学者。

您必须将app.services声明为app.services的依赖app.githubViewer

angular
    .module('app.githubViewer', ['app.services'])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM