[Fixed] Add variable in api request angular

Issue

I have a function that take a id as parameter and do :

onChooseVehicle(id){

      console.log(id);

      this.router.navigate(['tracking/v/-- id must be here --/live'])

  }

output of id : 4

that means i have successfully receive data, how i put this id in url here ?

      this.router.navigate(['tracking/v/ --HERE -- /live'])

Solution

You could either use template literals

this.router.navigate([`tracking/v/${id}/live`]);  // notice the back ticks `` instead of quotes ''

or string concatenation

this.router.navigate(['tracking/v/' + id + '/live']);  // here using quotes ''

You can’t mix them.

Leave a Reply

(*) Required, Your email will not be published