ng1 $rootScope dependency injected into ng2 getting an issue in aot (ng1 -> ng2 hybrid)

213 views Asked by At

I have created the following RootScopeService wrapper of $rootScope as below and after that i have used this under angular2 service named for example 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);
    }
}

I have compiled with AOT and it is compiled without fail . After then created the build file with rollup. When running the build.js file Getting following issue

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
0

There are 0 answers