【问题标题】:Angular1 <-> Angular2 Bridge accessing $rootScopeAngular1 <-> Angular2 Bridge 访问 $rootScope
【发布时间】:2017-01-29 21:03:06
【问题描述】:

我使用以下升级/降级桥 https://angular.io/docs/ts/latest/guide/upgrade.htmlAngular1Angular2 并排放置在一个应用程序中。

我的问题是是否有可能从 Angular2 服务访问 Angular1 $rootScope。需要明确的是,我不是在询问Angular2$rootScope 的等价物是什么。

【问题讨论】:

    标签: angularjs angular angular2-upgrade


    【解决方案1】:

    只需要:

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

    您无需升级任何东西,默认情况下它就在那里。见http://www.spaprogrammer.com/2017/03/inject-rootscope-into-angular-2.html

    使用现在的 recommended 在 Angular 6 Hybrid 上进行测试:

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

    您也可以使用$scope 进行此操作

    @Inject('$scope') private $scope: IScope
    

    【讨论】:

      【解决方案2】:

      所以我会回答我自己的问题。

      您必须通过以下方式升级$rootScope

      upgradeAdapter.upgradeNg1Provider('$rootScope')
      

      然后它可以像下面这样注入到Angular2服务中:

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

      【讨论】:

      • 嘿,我正在尝试这样做,但它说 $rootScope is not defined...:( 还有什么我需要处理的吗?
      猜你喜欢
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-17
      • 2017-05-09
      • 1970-01-01
      相关资源
      最近更新 更多