본문 바로가기

IT

[angular] retrieve the data as number


this code looks like no problem. but actually it could be error.

this.route.params.subscribe(
(params:Params) => {
this.server = this.serversService.getServer(params['id']);
}
);

let's look at this method. >> getServer(id:number)

the parameter of getserver is for 'number'


but the data from route.params is always string. so it makes the error.

we can fix this with just one typing of '+'

'+' cast the variable from string to number.

this.route.params.subscribe(
(params:Params) => {
this.server = this.serversService.getServer(+params['id']);
}
);


'IT' 카테고리의 다른 글

[angular] Handling queryParams  (0) 2018.03.23
[angular] router nested  (0) 2018.03.22
[angular] QueryParameters and Fragments  (0) 2018.03.21
[angular] route and parameter  (0) 2018.03.19
[angular] Router navigate relatively  (0) 2018.03.19