我正在尝试使用 angularjs 和 typescript 构建一个 Web 应用程序,但我完全无法将服务注入我的一个控制器。
服务
var mod= angular.module("servicemodul", []);
class Service
{
http: ng.IHttpService;
q: ng.IQService;
private user: User;
static $inject = ["$http", "$q"];
constructor($http: ng.IHttpService, $q:ng.IQService)
{
}
}
mod.service('Service',["$http", "$q", function ($http, $q) {
return new Service($http, $q);
}]);
控制器
angular.module("app", ["servicemodul"])
class MainController
{
scope: ng.IScope;
http: ng.IHttpService;
service: Service;
static $inject = ["$scope", "$http", "Service"];
constructor($scope: ng.IScope, $http: ng.IHttpService, service: Service)
{
this.http = $http;
this.scope = $scope;
this.Service= service;
(<any>this.scope).Main = this;
}
public Init()
{
}
}
在加载实际页面时,开发者控制台显示:
Unknown provider: ServicePorvider <- Service
问题一定是在模块中注册了服务的部分,我已经尝试过 module.factory() 方法,但它导致了同样的错误。我还确保所有生成的 js 文件都以正确的顺序加载(控制器之前的服务)。
另一个小问题:为服务和控制器设置不同的模块是否有意义,或者是否可以将所有内容放在一个模块中(假设整个应用程序很小)