how to use the $location.search in angular 4
var queryParams = $location.search(); -- in angular
setDefaultLanguage() {
var queryParams = $location.search();
if (queryParams.locale) {
var locale = queryParams.locale;
var selectedLanguageArray = languageOptions.filter(function (lang:any) {
return String(lang.id).toLocaleLowerCase() === String(locale).toLocaleLowerCase();
});
}
you can use the activated route:
export class AppComponent {
constructor(route: ActivatedRoute) {
route.queryParams.pipe(
filter(q => !!q.locale),
map(q => q.locale as string),
distinctUntilChanged()
).subscribe(locale => console.log(locale));
}
}
Or if you only want to have it onces you can get it from the snapshot:
if (route.snapshot.queryParams.locale) {
const locale = route.snapshot.queryParams.locale;
const selectedLanguageArray = languageOptions.filter(function (lang: any) {
return String(lang.id).toLocaleLowerCase() === String(locale).toLocaleLowerCase();
});
}