RouterLink array

I have a hyperlink where I need to make a routerlink in Angular 4. I have a lot of details for the url that the array is part of. I'm not sure how to make the array split into pieces for the routerlink array.

Take this contrived example:

[routerLink]="['/customers', customerid, orderid, arrayofints, anotherint]"

      

I would like the router to look like this: customerid = 10, order = 500, arrayofints = [1,2,3], anotherint = 888

"/customers/10/500/1/2/3/888"

      

I end up with something like this:

"/customers/10/500;0=1;1=2;2=3/888"

      

+3


source to share


2 answers


For something non-trivial, I would do the work in the component code, not in the template. So in the template:

[routerLink]="customerRouteFor(anotherint)"

      

and in a component where you can use the syntax ...spread

:



customerRouteFor(anotherint: number): (string | number)[] {
  return ['/customers', this.customerid, this.orderid, ...this.arrayofints, anotherint];
}

      

This seems cleaner than the approach concat

in my opinion.

+2


source


Flattening the arguments will give you the correct url



[routerLink]="[].concat.apply([],['/customers', customerid, orderid, arrayofints, anotherint])"

      

+2


source







All Articles