title: Angular condition in type provider with AOT
body: <p>I have an Angular project which I compile with AOT. I want to be able to register <code>ClassProvider</code> that is resolved dynamically according to configuration. Simplified code I use is this:</p>
  
  <pre><code>const isMock = Math.random() &gt; 0.5;
  
  @NgModule({
    // ...
    providers: [
      { provide: MyServiceBase, useClass: (isMock) ? MyServiceMock : MyService },
    ],
    bootstrap: [AppComponent]
  })
  export class AppModule { }
  </code></pre>
  
  <p>The problem is when I compile this with AOT I always get the same service. I would expect to get different service while hitting F5 (because of the <code>randomness</code> on the first line). When compiling without AOT it behaves as I expect.</p>
  
  <p>Here is the whole code example on github: <a href="https://github.com/vdolek/angular-test/tree/aot-conditioned-provider-problem" rel="noreferrer">https://github.com/vdolek/angular-test/tree/aot-conditioned-provider-problem</a>. It behaves differently with <code>ng serve</code> and <code>ng serve --aot</code>.</p>
  
  <p>How can I achieve this? I know I could use <code>FactoryProvider</code>, but then I would have to duplicate the services dependencies (parameters of the factory function and deps property on the <code>FactoryProvider</code>).</p>