Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular paramMap.getAll - clarification?

Angular 4 added support for paramMap with get and getAll methods :

I can understand this code which yields the "id" value

route.paramMap.subscribe(
     params => this.productID = params.get('id')
     );

But I don't understand the usage for getAll(?) :

getAll(name: string): string[] Return an array of values for the given parameter name.

I've seen here an example which states :

params:string[] = this.route.snapshot.paramMap.getAll();

this interface offers methods like get() to get specific value and getAll() to get all values.

I can understand the "get all values" part. but I don't understand the "get all values BY NAME" - which is getAll(name: string)

Question:

When would I use the getAll(param) method ? I mean I will/should only have unique parameter/s names. So how come getAll("id") can yield more than one value ?

An example would be much appreciated.

like image 780
Royi Namir Avatar asked Jan 29 '23 20:01

Royi Namir


1 Answers

You can use an array as a param, so if you navigate like this

this.router.navigate(['example', { foo: ['bar', 'baz'] } ]);

The you can use the getAll() function this way:

getAll("foo")

It will return all the param values: ['bar', 'baz']

like image 101
Gianluca Paris Avatar answered Jan 31 '23 23:01

Gianluca Paris