Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular1 <-> Angular2 Bridge accessing $rootScope

I use the following upgrade/downgrade bridge https://angular.io/docs/ts/latest/guide/upgrade.html to have Angular1 and Angular2 side by side in one application.

My question is if there is any possiblity to access Angular1 $rootScope from the Angular2 service. To be clear I am not asking about what is the equivalent for $rootScope in Angular2.

like image 540
Łukasz Rzeszotarski Avatar asked Sep 21 '16 14:09

Łukasz Rzeszotarski


2 Answers

All that's needed is:

@Injectable()    
class ExampleAngular2Service {
  constructor(@Inject('$rootScope') private _rootScope: IRootScopeService) {}
}

You don't have to upgrade anything, it's there by default. See http://www.spaprogrammer.com/2017/03/inject-rootscope-into-angular-2.html

Tested on Angular 6 Hybrid using the now recommended:

import { UpgradeModule } from '@angular/upgrade/static';

You can also do this with $scope

@Inject('$scope') private $scope: IScope
like image 200
MotKohn Avatar answered Nov 15 '22 18:11

MotKohn


So I will answer my own question.

You have to upgrade the $rootScope in the following manner:

upgradeAdapter.upgradeNg1Provider('$rootScope')

Then it can be injected in the Angular2 service like follows:

@Injectable()    
class ExampleAngular2Service {
    constructor(@Inject('$rootScope') private _rootScope: any) {}
}
like image 44
Łukasz Rzeszotarski Avatar answered Nov 15 '22 20:11

Łukasz Rzeszotarski