Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use arrays in routes with Angular?

Tags:

angular

I thought using getAll with a paramMap could help with getting arrays from the URL but apparently not.

Am I using it wrong?

Here's an example : https://stackblitz.com/edit/angular-bjrjz9

I navigate with this

this.router.navigate(['two', {
  id: [1, 2, 3, 4, 5]
}]);

And then I get the values

this.route.paramMap.subscribe(params => {
  const ids = params.getAll('id');
  console.log('ids', ids);
});

console.log says ['1,2,3,4,5'] but I expected it to be [1,2,3,4,5] or even ['1', '2', '3', '4', '5']

I'm just trying to pass an array to a route and then get the array back at the destination.
Of course, I could just split the string but what's the point of having getAll in that case? :)

like image 809
MrVoodoo Avatar asked Jan 22 '19 13:01

MrVoodoo


People also ask

What is pathMatch in Angular routing?

Angular 2 applications require to redirect route and pathMatch is a property which informs a router how to match and map a URL to the path of an actual route. In the application, the router automatically select the route using component HeroListComponent only when the entire URL of matches for example-

What is redirectTo in Angular?

redirectTo: '/home': We are using this property within the routes array to tell angular route service if the users navigate to the empty URL they should be redirected to the home URL rather than the empty URL.


1 Answers

The getAll() method returns duplicate parameters as an array.

Example:

http://www.example.com/?query=a&query=b&query=c

The value would be:

console.log(params.getAll('query')); // prints ["a","b","c"]

All query parameters in the HTTP specification are strings.

like image 145
Reactgular Avatar answered Sep 30 '22 08:09

Reactgular