cpiock
cpiock

Reputation: 1304

How to update Angular2 new router querystring parameters

In my component, I have a google map. If a user changes the center of the map, I would change my URL and add the new center lat-long in the URL so users can send it as a link. Something like this: http://myurl.com/map?center=lat!long

I use the new router of RC1

Upvotes: 0

Views: 1845

Answers (3)

KarateJB
KarateJB

Reputation: 961

Routes

const appRoutes: Routes = [
{ path: 'map/:id', component: mapComponent }]; //id is optional

Navigate the route

this.router.navigate(['map', '1', { 'center': 'taiwan'  }]);

And the uri will be like this :

http://localhost:4200/map/1;center=taiwan

Map component

import {Router, ActivatedRoute} from '@angular/router';
export class mapComponent implements OnInit {
    constructor(
        private route: ActivatedRoute) {
    }    
    ngOnInit() {
        //Get route parameter
        this.route.params.subscribe(params => {
            let custIdValue = params['id'];
            let centerValue = params['center'];
            //Do somthing...
        });
    }
}

Reference : Angular Doc

Upvotes: 1

maxisam
maxisam

Reputation: 22705

the correct syntax now should be

this._router.navigate(['.'], {
            queryParams: { center: this.variable },
            relativeTo: this._route
        });

Doc at Angular.io

Upvotes: 2

Patricia
Patricia

Reputation: 71

You could use:

this.router.navigate( [
  'CurrentPage', { id: 'companyId', param1: 'value1'
}]);

Upvotes: 0

Related Questions