Injecting Across Frameworks

Angular 1.x providers/services can be upgraded and injected into Angular 2.

Simple Angular 1.x service:

  1. export class A1UpgradeService {
  2. data = 'Hello from Angular 1 service';
  3. }

Simple Angular 2 component that will have an Angular 1.x service injected into it:

  1. import {Component, Inject} from '@angular/core';
  2. import {A1UpgradeService} from '../services/a1-upgrade-service';
  3. @Component({
  4. selector: 'a2-using-a1-service',
  5. template: `<p>{{ message }}</p>`
  6. })
  7. export class A2UsingA1Service {
  8. message = '';
  9. constructor(@Inject('a1UpgradeService') a1UpgradeService:A1UpgradeService) {
  10. this.message = a1UpgradeService.data;
  11. }
  12. }

Attaching everything to Angular 1.x:

  1. import {A2UsingA1Service} from './components/a2-using-a1-service';
  2. import {A1UpgradeService} from './services/a1-upgrade-service';
  3. // Angular 1 Vendor Import
  4. import * as angular from 'angular';
  5. // Import the upgradeAdapter singleton
  6. import {upgradeAdapter} from './upgrade-adapter';
  7. // Name the application
  8. const APPNAME = 'angular-upgrade-example';
  9. // Register classic Angular 1 modules
  10. angular
  11. .module(APPNAME)
  12. .directive('a2UsingA1Service',
  13. upgradeAdapter.downgradeNg2Component(A2UsingA1Service))
  14. .service('a1UpgradeService', A1UpgradeService);

Angular 2.x services can be downgraded and injected into Angular 1. In normaloperation, Angular 2.x services would be bootstrapped with the application, butbecause of ng-upgrade being a hybrid mode, this is not the case. The upgradeadapter comes with an addProvider method that must be used in the interim.

Here is a very simple Angular 2 service:

  1. import {Injectable} from '@angular/core';
  2. @Injectable()
  3. export class A2DowngradeService {
  4. fetchData() {
  5. return 'some data';
  6. }
  7. }

Since Angular 2 is bootstrapped with the upgrade adapter, there is no place toregister Angular 2 services. Fortunately the upgrade adapter's addProvidermethod can do this:

  1. upgradeAdapter.addProvider(Phones);

Lastly, Angular 1.x must be informed about the Angular 2 service:

  1. // The service to downgrade
  2. import {A2DowngradeService} from './services/a2-downgrade'
  3. // Angular 1 Vendor Import
  4. import * as angular from 'angular';
  5. // Import the upgradeAdapter singleton
  6. import {upgradeAdapter} from './upgrade-adapter';
  7. // Name the application
  8. const APPNAME = 'angular-upgrade-example';
  9. // Register classic Angular 1 modules
  10. angular
  11. .module(APPNAME)
  12. .factory('a2DowngradeService',
  13. upgradeAdapter.downgradeNg2Provider(A2DowngradeService));

Using this downgraded service in an Angular 1.x directive is as simple as:

  1. import {A2DowngradeService} from '../services/a2-downgrade';
  2. export function a1UsingA2ServiceDirective() {
  3. return {
  4. restrict: 'E',
  5. scope: {},
  6. bindToController: {},
  7. controller: A1UsingA2,
  8. controllerAs: 'a1UsingA2',
  9. template: `<span>{{ a1UsingA2.message }}</span>`
  10. };
  11. }
  12. class A1UsingA2 {
  13. message: string;
  14. constructor(private a2DowngradeService: A2DowngradeService) {
  15. this.message = this.a2DowngradeService.fetchData();
  16. }
  17. }

原文: https://angular-2-training-book.rangle.io/handout/migrate/ng-upgrade/injecting_across_frameworks.html