0
I wonder if my URL has number (ID parameter).
get this:
rota = "/pessoa/editar/5";
after the substring
rota = "/pessoa/editar";
validarRota(rota) { if(!isNaN(rota)) { rota = rota.substring(0, rota.lastIndexOf("/")); } }
But it doesn’t enter the if
of isNaN
.
What am I doing wrong?
Utilizo Angular 8.2
when using
!isNaN
you want to check if it is a number. To check when it is not a number, remove the!
.– Sam
Man, Sam is right, you’re denying the return of function incorrectly.
– Daniel Mendes
The function
isNaN
does not serve to check if it has number. It serves to check whether or not it is a number.– Sam
then without the
!
, on other routes which have no number it is also entering if, for examplepessoa/lista
stayspessoa
– LeoHenrique
Assuming that this
5
is a path parameter, it would not be better to useActivatedRoute
and check if the parameter is there (and if so, you see if only it is a number, without having to check the entire route)?– hkotsubo
Testing
isNaN('/pessoa/editar/5')
andisNaN('/pessoa/editar')
, both returntrue
. That means the condition! isNaN(rota)
isfalse
for these route values, then you will never enter theif
. The functionisNaN
evaluates the whole string - just because it has a number on it, doesn’t mean that the whole string is also a number.– hkotsubo
@hkotsubo, I’m not doing it directly where the route is... I do other things with the route in another Component and within this Component I need to do it up...
– LeoHenrique