15

I'm trying to create an application with angular 2,And Want pass params to tag a in [routerLink],i want craete a link like this :

<a href="/auth/signup?cell=1654654654"></a>

i dont know how to pass cell in template...

Kamil Naja
  • 6,267
  • 6
  • 33
  • 47

5 Answers5

13

You can work with queryParams which works with routerLink to build the url. For ex:

<a [routerLink]="['/profiles']" 
[queryParams]="{min:45,max:50,location:29293}">
  Search
</a>

This will build a route like http://localhost:3000/profiles?min=45&max=50&location=29923

Good Luck.

Aakash
  • 21,375
  • 7
  • 100
  • 81
10

If your are going to use angula2 beta then you have to send parameter like this while doing routing.

<a [routerLink]="['signup',{cell:cellValue}]">Routing with parameter</a>                        
<input type='text' [(ngModel)]='cellValue'>

and than in the receiving side than you have to get parameter via using RouteParams.

nut if You are going to use angular2 RC than you have to use RouteSegment instead of using RouteParams in angular2 RC. like this :-

import { Component } from '@angular/core';

import { Routes, RouteSegment, ROUTER_DIRECTIVES } from '@angular/router';

@Component({
  selector: 'about-item',
  template: `<h3>About Item Id: {{id}}</h3>`,
  Directives: [ROUTER_DIRECTIVES]
})

class AboutItemComponent { 

  id: any;

  constructor(routeSegment: RouteSegment) {
    this.id = routeSegment.getParam('id');    
  }
}

@Component({

    selector: 'app-about',

    template: `

      <h2>About</h2>
        <a [routerLink]="['/about/item', 1]">Item 1</a>
        <a [routerLink]="['/about/item', 2]">Item 2</a>
      <div class="inner-outlet">
        <router-outlet></router-outlet>
      </div>
    `,
    directives: [ROUTER_DIRECTIVES]
})

@Routes([

  { path: '/item/:id', component: AboutItemComponent }

])

export class AboutComponent { }
Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215
2

you can try the code below: Your ts file will be like this:

@Component({ ... })
@Routes([
    {
        path: '/auth/signup?cell=1654654654', component: AuthComponent
    }
])
export class AppComponent  implements OnInit {
    constructor(private router: Router) {}
}

And in you html file,

<a [routerLink]="['//auth/signup?cell=1654654654']"></a>
Tarang Bhalodia
  • 1,185
  • 11
  • 21
2

In Angular2 supports both query parameters and path variables within routing.

use like:

<a [routerLink]="['Signup', {cell: '1654654654'}]">Signup</a>

and in component:

@RouteConfig([
    new Route({ path: '/auth/signup', component: SignupComponent, name: 'Signup'})
])

then shown in url like that you want /auth/signup?cell=1654654654

NB:

If in your path contain cell in component as params like: /auth/signup/:cell and routelink like: [routerLink]="['Signup', {cell: '1654654654'}]" then url will show like: auth/signup/1654654654

Shaishab Roy
  • 16,335
  • 7
  • 50
  • 68
2

The accepted answer is outdated and IMHO doesn't answer the question.

However the question is not clear: "route params" are in the path part of the URL but the question was about "query params" which are after the '?' (question mark).

So the answer to the question is in the answer of @Akash: you have to use

[queryParams]="{name: value}"

in the template.

So this anchor will reference to /path/with/myparam?cell=1654654654:

<a [routerLink]="['/path/with/:paramName', {paramName: "myparam"}]"
   [queryParams]="{cell: 1654654654}"
></a>
Manuel Fodor
  • 464
  • 4
  • 6