Skip to content

Instantly share code, notes, and snippets.

@tkutcher
Last active January 10, 2022 02:20
Show Gist options
  • Save tkutcher/436a179fbadc5bd2d94819e1e4fb79ec to your computer and use it in GitHub Desktop.
Save tkutcher/436a179fbadc5bd2d94819e1e4fb79ec to your computer and use it in GitHub Desktop.
Environment-Specific Angular Configuration - Add Injection Token
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { MY_API_BASE } from './my-api.service';
import { environment } from '../environments/environment';
@NgModule({
declarations: [ AppComponent ],
imports: [ BrowserModule ],
providers: [
{ provide: MY_API_BASE, useValue: environment.apiAddress }
],
bootstrap: [AppComponent]
})
export class AppModule { }
export const MY_API_BASE = new InjectionToken<string>("My API Address");
@Injectable({ providedIn: "root" })
export class MyApiService {
constructor(
private http: HttpClient,
@Inject(MY_API_BASE) private apiAddress: string
) {}
getFoo(): Observable<any> {
return this.http.get(`${this.apiAddress}/foo`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment