打字稿角度未知的提供者问题

时间:2015-05-28 13:00:56

标签: angularjs angular-services typescript1.5

在Controller中注入StateService时遇到此问题

  

错误:[$ injector:unpr]未知提供者:StateServiceProvider< -   的StateService

//Service
module ba.entry {
  export class StateService{
        constructor () {}
  }

  angular
      .module('ba.entry')
      .service('ba.entry.StateService', StateService);
}


// Controller
module ba.entry {

    export class StateController {

        static $inject = ['$scope', 'ba.entry.StateService'];

        constructor (public $scope: Scope, stateService) {}
    }

    angular
        .module('ba.entry')
        .controller('ba.entry.StateController', StateController);

}

// App Configuration

module ba {

    'use strict';
        angular.module('ba.entry', []);

        angular
            .module('betting-assistance', [
                'ui.router',
                'AutoCompleteApp'
            ]);
}

2 个答案:

答案 0 :(得分:0)

您正在创建这样的服务:

.service('ba.entry.StateService', StateService);

并且您的$ inject语句定义如下

static $inject = ['$scope', 'StateService'];

问题是$ inject会在注册到angular模块时使用正在使用的字符串,因此将inject语句更改为...

static $inject = ['$scope', 'ba.entry.StateService'];

我看到的另一个问题是你试图将服务注入模块......

angular.module('ba.entry', ['ba.entry.StateService']);

应该是:

angular.module('ba.entry', []);

答案 1 :(得分:0)

模块

  

ba.entry

未加载为依赖

  

投注协助

module ba {

    'use strict';
        angular.module('ba.entry', []);

        angular
            .module('betting-assistance', [
                'ui.router',
                'ba.entry',
                'AutoCompleteApp'
            ]);
}