使用Injector类代替ReflectiveInjector类

时间:2018-09-06 14:06:42

标签: angularjs angular angularjs-scope

我正在尝试动态加载角度数据。在我的方法中,我使用下面的代码加载数据-

 const inputProviders = Object.keys(data.inputs).map(inputName => {
  return { provide: inputName, useValue: data.inputs[inputName] };
});
const resolvedInputs = ReflectiveInjector.resolve(inputProviders);

const injector = ReflectiveInjector.fromResolvedProviders(
  resolvedInputs,
  this.cardComponent.parentInjector
);

const componentType = this.content.resolve(data.component);
const factory = this.resolver.resolveComponentFactory(componentType);

const component = factory.create(injector);

this.cardComponent.insert(component.hostView);

Lint指出ReflectiveInjector将被贬值。我应该如何使用Injector,以使当前代码正常工作。 我尝试了下面几页中提到的方法,但是它没有说明resolve()和fromResolvedProviders()方法。

Link 1 Link 2

1 个答案:

答案 0 :(得分:0)

我找到了答案。以下是我使用的代码-

 const inputProviders = Object.keys(data.inputs).map(inputName => {
  return { provide: inputName, useValue: data.inputs[inputName] };
});

const injector = Injector.create(
 { providers: inputProviders,
  parent : this.cardComponent.parentInjector
});

const componentType = this.content.resolve(data.component);
const factory = this.resolver.resolveComponentFactory(componentType);

const component = factory.create(injector);

this.cardComponent.insert(component.hostView);

在这里1 2