使用概述的过程here https://angular.io/docs/ts/latest/guide/upgrade.html#!#making-angularjs-dependencies-injectable-to-angular,我正在尝试将 Angular 1 服务注入 Angular 4 应用程序中。该应用程序以混合模式引导(并且在我运行一些 Angular 4 组件和服务时工作)。
每当我尝试注入 Angular 1 服务时,我都会得到无法读取未定义的属性“get”.
升级后的providers.ts:
import {LinksService} from "./+services/links/links";
export function linksServiceFactory(i: any) {
return i.get('bfLinksService'); // <--- Errors here!
}
export const linksServiceProvider = {
provide: LinksService,
useFactory: linksServiceFactory,
deps: ['$injector']
};
我尝试使用 LinksService 的 Angular 4 服务如下所示:
@Injectable()
export class EntityService {
constructor (
private http: Http,
private links: LinksService
) {
}
get(id: string): Observable<OrgDetails> {
// Do something
}
}
最后,LinksService(Angular 1 服务,用 Typescript 编写)如下所示:
export class LinksService {
static $inject = ["$log", "$location", PROPERTIES_SERVICE];
private contentHost : string;
private thisAppHost : string;
constructor (private $log : ng.ILogService, private $location : ng.ILocationService, private props : IPropertiesService) {
this.init();
}
// Service functions elided
}
引导程序和模块的内容:
@NgModule({
imports: [
BrowserModule,
HttpModule,
UpgradeModule,
],
declarations: [
AppComponent,
OrgSummaryComponent,
],
providers: [
EntityService,
linksServiceProvider
],
bootstrap: [
AppComponent,
],
})
export class AppModule {
ngDoBootstrap() {
// Does nothing by design.
// This is to facilitate "hybrid bootstrapping"
}
}
platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
upgrade.bootstrap(document.body, [AppModuleName], {strictDi: false});
});
Angular 1(旧版)的东西都工作得很好。
看起来 Angular 找不到 $injector,但无论如何它不应该在那里吗?
非常感谢您的任何建议,
杰夫