My current url is:
https://localhost:8080/MyApp/manager/dashboard
Within the above url I want to fetch only https://localhost:8080/MyApp/ part.
Is there any way to fetch this without hard coding anything?
Join Stack Overflow to learn, share knowledge, and build your career.
My current url is:
https://localhost:8080/MyApp/manager/dashboard
Within the above url I want to fetch only https://localhost:8080/MyApp/ part.
Is there any way to fetch this without hard coding anything?
window.location contains lots of information. Suppose the URL is https://example.com:80/a/b/c, you can get following information easily in Angular:
window.location.hostname = "example.com"
window.location.host = "example.com:80"
window.location.protocol = "https:"
window.location.port = "80"
window.location.pathname = "/a/b/c"
window.location.href = "https://example.com:80/a/b/c"
If you want to get the domain name and application name only, rely on the router location and on the window location.
import { Location } from '@angular/common';
constructor(private loc: Location) {}
ngOnInit() {
const angularRoute = this.loc.path();
const url = window.location.href;
const domainAndApp = url.replace(angularRoute, '');
}
const domainAndApp = url.replace(`#${angularRoute}`, '');
is more suitable :)
– Rochadsouza
Oct 21 '19 at 14:07