0

I am using VS 2017 (framework 2.7) and Angular 5 to develop my application. I want to get the parameter value from url. I have written the following in app.module.ts:

RouterModule.forRoot([
      { path: '', component: HomeComponent, pathMatch: 'full' },
      { path: 'counter', component: CounterComponent },
      { path: 'fetch-data', component: FetchDataComponent },
      { path: 'create-strings', component: CreateStringsComponent },
      { path: 'strings/edit/:id', component: CreateStringsComponent },
      { path: 'fetch-strings/:prop', component: StringsComponent },
      { path: 'fetch-strings/:prop', component: StringsComponent },
      { path: 'fetch-strings/:prop', component: StringsComponent }

      //{ path: '**', redirectTo: 'home' }  
    ])
  ],

The nav-menu.component.html menu file is like:

<li [routerLinkActive]='["link-active"]'>
              <a [routerLink]="['/fetch-strings']" [queryParams]="{prop: 'loc'}" (click)='collapse()'>
                <span class='glyphicon glyphicon-th-list'></span> Strings
              </a>
            </li>
            <!--https://stackoverflow.com/questions/37880876/how-to-pass-query-parameters-with-a-routerlink-in-the-new-router-v-3-alpha-vlad-->
            <li [routerLinkActive]='["link-active"]'>
              <a [routerLink]="['/fetch-strings']" [queryParams]="{prop: 'dep'}" (click)='collapse()'>
                <span class='glyphicon glyphicon-th-list'></span> Department
              </a>
            </li>
            <li [routerLinkActive]='["link-active"]'>
              <a [routerLink]="['/fetch-strings']" [queryParams]="{prop: 'des'}" (click)='collapse()'>
                <span class='glyphicon glyphicon-th-list'></span> Designation
              </a>
            </li>

I want to capture the parameter value and change the and title of the page accordingly. For that purpose I have written the following code in the Strings.Component.ts file:

 constructor(public http: Http, private _router: Router, private _stringsService: StringsService, private _activeRoute: ActivatedRoute) {
        this.getStrings();
    }

    //https://stackoverflow.com/questions/45309131/angular-2-4-how-to-get-route-parameters-in-app-component
    ngOnInit() {
        debugger;
        //console.log(this._activeRoute.snapshot.params['prop']);

        //this.var1 = this._activeRoute.snapshot.params['prop'];
        //this.var2 = this._activeRoute.params.subscribe(params => { this.var1 = params['prop']; });

        this._activeRoute.queryParams.subscribe((prop: Params) => {
            console.log(prop);
        });

        if (this.var1 == "loc") {
            this.title = "Location";
        }
        else if (this.var1 == "dep") {
            this.title = "Department";
        }
        else if (this.var1 == "des") {
            this.title = "Designation";
        }
    }

The code:

this._activeRoute.queryParams.subscribe((prop: Params) => {
            console.log(prop);
        });

is showing the parameter value properly. 1) But how can I store the parameter value in a variable so that I can use it to change the title dynamically.

this.var1 = this._activeRoute.snapshot.params['prop'];

is not working.

2) Second problem, When I click on the other links (nav-menu.component.html file), the first menu always remains selected.

Any clue?

Thanks

Partha

Partha
  • 469
  • 2
  • 14
  • 32

3 Answers3

0

Remove input property binding from routerLinkActive Directive

  <li routerLinkActive='["link-active"]'>
                  <a [routerLink]="['/fetch-strings']" [queryParams]="{prop: 'loc'}" (click)='collapse()'>
                    <span class='glyphicon glyphicon-th-list'></span> Strings
                  </a>
                </li>
                <!--https://stackoverflow.com/questions/37880876/how-to-pass-query-parameters-with-a-routerlink-in-the-new-router-v-3-alpha-vlad-->
                <li routerLinkActive='["link-active"]'>
                  <a [routerLink]="['/fetch-strings']" [queryParams]="{prop: 'dep'}" (click)='collapse()'>
                    <span class='glyphicon glyphicon-th-list'></span> Department
                  </a>
                </li>
                <li routerLinkActive='["link-active"]'>
                  <a [routerLink]="['/fetch-strings']" [queryParams]="{prop: 'des'}" (click)='collapse()'>
                    <span class='glyphicon glyphicon-th-list'></span> Designation
                  </a>
                </li>

If you want to get prop value from params then you have to be in that url otherwise you will data undefined or null

this._activeRoute.snapshot.params['prop'];

If you want to get queryParams use snapshot.queryParams

this._activeRoute.snapshot.queryParams['prop']
Chellappan வ
  • 23,645
  • 3
  • 29
  • 60
0

This might be helpful.

this.var1 = this._activeRoute.snapshot.paramMap.get('prop');

Reference angular.io

Hope this help you!!

Sanoj_V
  • 2,936
  • 1
  • 12
  • 28
  • I have tried this.var1 = this._activeRoute.snapshot.paramMap.get('prop');. But it is returning null. – Partha Aug 15 '18 at 16:51
  • @Partha console this `this._activeRoute.snapshot.params`. Check the params what it gives in console. – Sanoj_V Aug 15 '18 at 17:24
  • this._activeRoute.snapshot.queryParams['prop'] ---> Showing 'var1 :undefined' but I can log the value like: this._activeRoute.queryParams.subscribe((prm: Params) => { console.log(prm); }); – Partha Aug 15 '18 at 18:08
0

The following code has solved my problem:

this.var1 = this._activeRoute.snapshot.queryParams.prm;

prm -> is name of the parameter.

Thanks

Partha
  • 469
  • 2
  • 14
  • 32