본문 바로가기

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' 카테고리의 다른 글