Posts

Showing posts with the label Routes

Angular 2 Get Current Route

Answer : Try this, import { Router } from '@angular/router'; export class MyComponent implements OnInit { constructor(private router:Router) { ... } ngOnInit() { let currentUrl = this.router.url; /// this will give you current url // your logic to know if its my home page. } } Try it import { Component } from '@angular/core'; import { Router, NavigationEnd } from '@angular/router'; @Component({...}) export class MyComponent { constructor(private router:Router) { router.events.subscribe(event => { if (event instanceof NavigationEnd ) { console.log("current url",event.url); // event.url has current url // your code will goes here } }); } } Try any of these from the native window object. console.log('URL:' + window.location.href); console.log('Path:' + window.location.pathname); console.log('Host:' + window.location.host); console.log('Ho...

Angular 2 Router No Base Href Set

Image
Answer : https://angular.io/docs/ts/latest/guide/router.html Add the base element just after the <head> tag. If the app folder is the application root, as it is for our application, set the href value exactly as shown here. The <base href="/"> tells the Angular router what is the static part of the URL. The router then only modifies the remaining part of the URL. <head> <base href="/"> ... </head> Alternatively add >= Angular2 RC.6 import {APP_BASE_HREF} from '@angular/common'; @NgModule({ declarations: [AppComponent], imports: [routing /* or RouterModule */], providers: [{provide: APP_BASE_HREF, useValue : '/' }] ]); in your bootstrap. In older versions the imports had to be like < Angular2 RC.6 import {APP_BASE_HREF} from '@angular/common'; bootstrap(AppComponent, [ ROUTER_PROVIDERS, {provide: APP_BASE_HREF, useValue : '/' }); ]); < RC.0 ...