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.
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With