Check if router.url contains specific string?
You can use LocationStrategy to get url without parameters.
import {LocationStrategy} from '@angular/common';
export class AppComponent implements OnInit {
constructor(private url:LocationStrategy) { }
ngOnInit() {
console.log(this.url.path());
if(this.url.path()==='/test/sort'){
this.active=0;
}
}
}
You can also use:
if (this.router.url.includes('/test/sort'))
{
this.active = 0;
}
Method String.includes(searchString: string, position?: number): boolean
- Returns true
if searchString
appears as a substring of the result of converting this object to a String, at one or more positions that are greater than or equal to position; otherwise, returns false.
If you want something basic that works:
if (this.router.url.indexOf('/test/sort') > -1) {
this.active = 0;
}