我正在使用这个起始模板,并按照这篇SO帖子中提出的修改进行了修改。一直很顺利,但现在我试图将一个服务注入到另一个服务的构造函数中时,注入的值总是'未定义'。没有抛出任何错误。
这里是一个简单的例子:
module app.services {
export class dashboardFilterService implements IService {
constructor($rootScope: ng.IRootScopeService) {
// $rootScope is undefined...
//$rootScope.$broadcast('FilterInitialized');
}
}
}
app.registerService('dashboardFilterService', ['$rootScope']);
将服务注入到控制器中是可行的。我对TypeScript和Angular都比较新,所以对于有更多经验的人来说可能很明显。以下是参考的编译后js代码:
var app;
(function (app) {
(function (services) {
var dashboardFilterService = (function () {
function dashboardFilterService($rootScope) {
// $rootScope is undefined...
//$rootScope.$broadcast('FilterInitialized');
}
return dashboardFilterService;
})();
services.dashboardFilterService = dashboardFilterService;
})(app.services || (app.services = {}));
var services = app.services;
})(app || (app = {}));
app.registerService('dashboardFilterService', ['$rootScope']);
export function registerService(className: string, services = []) { var service = className[0].toLowerCase() + className.slice(1); services.push(() => new app.services[className]()); angular.module('app.services').factory(service, services); }
- jdraper3