angularjs – 在启动混合角度1 2应用程序时,如何初始化角度1喷射
为了重新创建这个问题,我分叉了Angular Quickstart plunker.
我有这个简单的角度1.x模块: 'use strict'; angular.module('testModule',[]) .factory('testService',function($q,$log,$interval,$rootScope){ //Create a service object that we'll build up with methods and eventually //return. var service = {}; service.test = 'test'; return service; }); 我有这个角度2模块引用角度1.x $注入器来获得角度1.x模块作为提供者: import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { UpgradeModule } from '@angular/upgrade/static'; import { AppComponent } from './app.component'; @NgModule({ imports: [ BrowserModule,UpgradeModule ],declarations: [ AppComponent ],bootstrap: [ AppComponent ],providers: [{ provide: 'testModule',useFactory: (i: any) => i.get('testModule'),deps: ['$injector'] }] }) export class AppModule { ngDoBootstrap() {} } 我有我的角度2组件,通过依赖注入请求角度1.x模块: import { Component,Inject } from '@angular/core'; @Component({ selector: 'my-app',template: `<h1>Hello {{name}},my name is {{myName}}</h1>` }) export class AppComponent { name = 'Angular'; constructor(@Inject('testModule') testModule: any) { this.myName = testModule.test; } } 然后我引导角度2模块和角度1.x模块 import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app.module'; platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => { const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule; upgrade.bootstrap(document.body,['testModule'],{strictDi: true}); }); 然后我得到这个错误TypeError:无法读取未定义的属性’get’ providers: [{ provide: 'testModule',deps: ['$injector'] }] 我认为有一些我不了解如何在混合角度1 2应用中初始化角度1.x喷射器.您可以看到我在通过脚本标记中拉出角度1.x,否则我的角度1.x模块将抛出关于“角度”未定义的错误,我怀疑问题可能与此有关.如果有人能提供指导,我会很感激. 这是羽毛球:https://plnkr.co/edit/wdR7K4rDKoF8L0pfQypM?p=info 这是调用堆栈的错误:
解决方法
我遇到了同样的问题,并在
github上为我的案例找到了解决方案.
请查看更新的 plunker.请注意,您的代码中还有其他不相关的问题.评论< script src =“test-module.js”>< / script>和testService的隐式注入参数. 希望它会对你有所帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |