Malik Kashmiri Malik Kashmiri - 2 months ago 14
TypeScript Question

How get value from url in angular 2

I am trying to get value but it not working. below is my url I want to get

1
from url as number


https://localhost:44300/Portal#/user-edit/1


constructor(private router: Router,
private userService: UserService) {

this.router
.routerState
.queryParams
.subscribe(params => {
this.id = params['id'];
});

Answer

You should inject ActivatedRoute to get route params as shown below,

constructor(private router: Router,
    private userService: UserService,
    private route: ActivatedRoute,) {  //<----- here

    this.route.params.subscribe(params => {
            this.id = +params['id'];   //<----- + sign converts string value to number
});

read more here : https://angular.io/docs/ts/latest/guide/router.html#!#route-parameters

Comments