Overriding at runtime

There is a way to decide what to inject for/into a construct at runtime. So far, we have been very explicit about when to override, but we can do this with a bit of logic added to it by using the useFactory keyword. It works like the following:

let factory = () => {  if(condition) {    return new FakeService();  } else {    return new Service();  }}@Component({ providers : [   { provide : Service, useFactory : factory } ]})

This factory can in itself have dependencies; we specify those dependencies with the deps keyword like so:

let factory = (auth:AuthService, logger: Logger) => {  if(condition) {    return new FakeService();  } else {    return new Service();  }  }@Component({  providers : [   { provide : Service, useFactory : factory, 

Get Architecting Angular Applications with Redux, RxJS, and NgRx now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.