Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing variable in Angular2

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]
like image 332
ThreeAccents Avatar asked Oct 06 '15 15:10

ThreeAccents


People also ask

What is routing in angular2?

Routing helps in directing users to different pages based on the option they choose on the main page. Hence, based on the option they choose, the required Angular Component will be rendered to the user. Let's see the necessary steps to see how we can implement routing in an Angular 2 application.

What are routing types in Angular?

Angular Router supports multiple outlets in the same application. A component has one associated primary route and can have auxiliary routes. Auxiliary routes enable developers to navigate multiple routes at the same time.

What is routing and navigation in Angular?

Angular provides extensive set of navigation feature to accommodate simple scenario to complex scenario. The process of defining navigation element and the corresponding view is called Routing. Angular provides a separate module, RouterModule to set up the navigation in the Angular application.


2 Answers

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 }
])
like image 87
Phil Avatar answered Oct 01 '22 16:10

Phil


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 :)

like image 29
Eric Martinez Avatar answered Oct 01 '22 18:10

Eric Martinez