Thursday, June 8, 2017

import {Injector} from '@angular/core';

I'm a little fuzzy on how Injector manages/assists dependency injection in an Angular 2 app. At a glance it seems you may have a child with a constructor like so:

export class MyOneOffWackiness extends MyUniversalDependencies {
   constructor(public foo: Foo, public bar: Bar, injector: Injector) {
      super(injector);
   }

 
 

The parent would then look like this:

export abstract class MyUniversalDependencies {
   foo: Foo;
   bar: Bar;
   constructor(public injector: Injector) {
      this.foo = injector.get(Foo);
      this.bar = injector.get(Bar);
   }

 
 

This basically just wires up the default Foo for foo and the default Bar for bar (as best as I can tell) but it also leaves the door open for one to double back to this and make something that inherits from Foo feed foo while also making a sweeping change for all of the children implementations in one place.

No comments:

Post a Comment