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