我已经创建了以下RootScope的$ rootScope包装器,之后我在angular2服务下使用了这个,例如xService。
import {Injectable, Inject} from "@angular/core";
@Injectable()
export class RootScopeService {
static instance: RootScopeService;
public $rootScope: any
// constructor( @Inject("$rootScope") public $rootScope: any)
constructor() {
return RootScopeService.instance = RootScopeService.instance || this;
}
public broadcast(eventName, event, scope) {
this.$rootScope.$broadcast(eventName, event, scope);
};
public getRootScope(): any {
return RootScopeService.instance.$rootScope;
};
}
import {Injectable, Inject} from '@angular/core'
import {Logger, Level} from 'angular2-logger/core'
@Injectable()
export class xService {
private log: Logger;
private static instance: xService;
constructor( @Inject("$rootScope") public rootScopeService: any) {
if (xService.instance == null) {
this.log = new Logger();
this.log.Level = Level.DEBUG;
xService.instance = this;
}
return xService.instance;
}
trackCustomEvent(customEvent, currentScope) {
try {
xService.instance.rootScopeService.broadcast(customEvent, currentScope);
} catch (ex) {
x.instance.log.error("x:: trackCustomEvent : " + ex.toString());
}
}
trackActionEvent(actionEvent, scope) {
xService.instance.trackCustomEvent(actionEvent, scope);
}
}
我已经使用AOT编译并且编译时没有失败。然后使用汇总创建构建文件。运行build.js文件时 获得以下问题
Uncaught TypeError: Cannot read property 'get' of undefined
rootScopeFactory @ build.js:31452
__rootScope_46 @ build.js:80486
_RootScopeService_53 @ build.js:80528
createInternal @ build.js:80897
NgModuleInjector.create @ build.js:9964
NgModuleFactory.create @ build.js:9938
(anonymous function) @ build.js:9492
e.invoke @ zone.min.js:1inner.inner.fork.onInvoke @ build.js:8897
e.invoke @ zone.min.js:1n.run @ zone.min.js:1NgZone.run @ build.js:8766
PlatformRef_._bootstrapModuleFactoryWithZone @ build.js:9490
PlatformRef_.bootstrapModuleFactory @ build.js:9473
(anonymous function) @ build.js:81349
(anonymous function) @ build.js:81355