Skip to content

Instantly share code, notes, and snippets.

@akleemans
Last active July 21, 2020 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akleemans/14c851b887dea01023f5c952d5418e63 to your computer and use it in GitHub Desktop.
Save akleemans/14c851b887dea01023f5c952d5418e63 to your computer and use it in GitHub Desktop.
import {Component, OnInit} from '@angular/core';
import {HttpClient} from "@angular/common/http";
@Component({
selector: 'app-root',
template: 'Called API and got: "{{greeting}}"'
})
export class AppComponent implements OnInit {
public greeting: string;
public constructor(private readonly http: HttpClient) {
}
public async ngOnInit(): Promise<void> {
// Check if development or prod deployment
let restUrl = location.origin + '/api/hello?name=Azure';
if (this.isDevelopment()) {
restUrl = 'http://localhost:8080/api/hello?name=Angular';
}
this.greeting = await this.http.get(restUrl, {responseType: 'text'}).toPromise();
}
private isDevelopment = (): boolean => location.host.includes('localhost');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment