Sharing Top Content from the Angular-sphere.

Angular deprecates ReflectiveInjector and introduces StaticInjector. Should you care?

  • But Angular also creates three other injectors for the Platform, Compiler and NgZone.
  • Well, remember this line you usually use in your first function call creates a platform and can take platform providers.
  • The second function instantiates the JIT compiler and can take providers for the compiler.
  • NgZoneInjector doesn’t take any providers so it’s unaffected by change.As I said you could pass providers to the platform injector like this:class B {}class A { constructor(@Inject(B) b) {} }platformBrowserDynamic([A, B])And to the compiler like this:class B {}class A { constructor(@Inject(B) b) {} }bootstrapModule(AppModule, {providers: [A, B]});Since the platform will migrate to static injector it will no longer be able to resolve implicit dependencies by metadata the way reflective injector did.
  • So you will need to refactor your code to the following for the platform:class B {}class A { constructor(b) {} }platformBrowserDynamic([{ provide: A, useClass: A, deps: [B] }, B])and the platform:class B {}class A { constructor(b) {} }bootstrapModule(AppModule, { providers: [ {provide: A, useClass: A, deps: [B]}, B ] });Again, as I said above the breaking change only affects the platform and compiler injector.

Angular updated change log where it mentioned the breaking change regarding platform and compiler providers and introduced static injector. It is a replacement for the existing Reflective Injector as…

@russfrith: Angular introduces StaticInjector. Should you care? by @maxim_koretskyi #angularjs #angular2

Angular updated change log where it mentioned the breaking change regarding platform and compiler providers and introduced static injector. It is a replacement for the existing Reflective Injector as the latter is now deprecated. Not many of us have used Reflective Injector in our code base directly and even fewer yet understand the intricacies of its internals. So it’s not immediately clear what to make of such a change.

Why is better? How does it affect my code? Do I need to migrate? These are the questions you might be asking yourself. But not to worry. Me and Alexey Zuev have got everything covered for you. This article will give you the answer you need.

Before we take a look at the new static injector let’s spend a few minutes and see how the currently used injector got its name. We will use this this simple example to do that:

. So how does injector know that?

. I’ve written in Implementing custom component decorator in Angular how decorators in Angular use this library.

decorator implementation:

function ParamDecorator(cls: any, unusedKey: any, index: number) {

// parameters here will be [ {token: B} ]

Reflect.defineMetadata(‘parameters’, parameters, cls);

gets executed we have the information about dependencies attached to the class.

function that takes a provider and resolves its dependencies:

shim to extract implicit dependencies.

we had the following:

we need to refactor the above code to the following:

There is static version of the provider classes now for every possible recipe:

is not deprecated so you can continue using it for now. But there’s a great probability that it will be removed in the future. So it’s a good idea to start migrating to new static injector.

doesn’t take any providers so it’s unaffected by change.

As I said you could pass providers to the platform injector like this:

And to the compiler like this:

Since the platform will migrate to static injector it will no longer be able to resolve implicit dependencies by metadata the way reflective injector did. So you will need to refactor your code to the following for the platform:

and the platform:

Again, as I said above the breaking change only affects the platform and compiler injector.

Angular deprecates ReflectiveInjector and introduces StaticInjector. Should you care?

Comments are closed, but trackbacks and pingbacks are open.