13

I've read over the documentation for the new Angular router. The example they have for routing to a variable is this:

Component/Module:

angular.module('myApp', ['ngFuturisticRouter'])
.controller('AppController', ['$router', function($router) {
  $router.config({ path: '/user/:id' component: 'user' });
  this.user = { name: 'Brian', id: 123 };
});

HTML/Template:

<div ng-controller="AppController as app">
  <a router-link="user({id: app.user.id})">{{app.user.name}}</a>
</div>

Here is my component:

@RouteConfig([
  { path: '/', component: Home, as: 'home' },
  { path: '/displays/:id', component: DisplayDetails, as: 'display-details' }
])

And my HTML/Template:

<div class="col-md-3" *ng-for="#display of displays">
    <a [router-link]="['/display-details({id: display.id})']"><display-card ></display-card></a>
</div>

I've also tried instead of putting the component alias (display-details) I've tried putting the actual path and the component it self but they all give me the same error:

EXCEPTION: Component "App" has no route named "display-details({id: display.id})". in [null]
3
  • 1
    Try with [router-link]="['/display-details', {id: display.id}]", btw, what version are you using? Commented Oct 6, 2015 at 16:52
  • @EricMartinez Thank you man! I'm using alpha.37. BTW you've really helped me out with Angular2 I think you've answered all the questions I've had Commented Oct 6, 2015 at 17:35
  • @EricMartinez You wanna make it an answer so I can give a check mark Commented Oct 6, 2015 at 17:36

4 Answers 4

20

That syntax works in my case:

<div class="col-md-3" *ng-for="#display of displays">
    <a [routerLink]="['/display-details', display.id]">
       <display-card ></display-card>
    </a>
</div>

with this router config:

@RouteConfig([
  { path: '/', component: Home, as: 'home' },
  { path: '/display-details/:id', component: DisplayDetails }
])
Sign up to request clarification or add additional context in comments.

Comments

14

As suggested by @ThreeAccents

Quoting from the documentation RouterLink.

RouterLink expects the value to be an array of route names, followed by the params for that level of routing. For instance ['/Team', {teamId: 1}, 'User', {userId: 2}] means that we want to generate a link for the Team route with params {teamId: 1}, and with a child route User with params {userId: 2}.

So the solution is to change your routerLink to as follows :

<div class="col-md-3" *ng-for="#display of displays">
    <a [routerLink]="['/display-details', {id: display.id}]">
       <display-card ></display-card>
    </a>
</div>

Glad it worked for you :)

2 Comments

I updated your answer to reflect the API as of 2.0-beta. I think it's still missing 2 things. The link /display-details should be /DisplayDetails and the route 'as' parameter has been changed to 'name'.
I had to use string concatenation like <a [routerLink]="['/display-details'+display.id]"> , but this helped me get most of the way there
8

For the path: '/user/:id'

We can use in two way like /user;id:1234 or /user/123

// For /user;id:1234
[routerLink]="['/user', {id: display.id}]"
// For /user/1234
[routerLink]="['/user', display.id]"

I came here for my issue with path: '/customers/:id/detail' and finally finished with:

[routerLink]="['/customers', cust.id, 'detail']"

Comments

0

You can use a function in your component to get the dynamic URL and params by using routerLink and queryParams directive.

<div class="col-md-3" *ng-for="#display of displays">
    <a [routerLink]="getURL(display)" 
       [queryParams]="getParams(display)>
       <display-card ></display-card>
    </a>
</div>

in component:

getURL(display) { 
    return ['/displays', display.id];
}

getParams(display) { 
    return { param: display.somethingElse }; 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.