0

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();
        });
    }
user1877936
  • 351
  • 3
  • 7
  • 22
  • Possible duplicate of [$location.search() equivalent in Angular 7](https://stackoverflow.com/questions/54772646/location-search-equivalent-in-angular-7) – nircraft Oct 31 '19 at 13:04

1 Answers1

1

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();
        });
    }
Marcel Hoekstra
  • 1,334
  • 12
  • 19