5

My web application has multiple filters that should be represented in the current Url so that users can simply copy/bookmark the current page to get it back later.

In angular1 I used $location.search(name, value) to simply set seach params on change. Now I want to get something similar in angular2.

Or is it wrong?

K. D.
  • 4,041
  • 9
  • 48
  • 72

1 Answers1

2

I think you must use the router inside Angular2. code example:

import {Component} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
import {ProfileComponent} from './profile.component';

@Component({
    selector: 'my-app',
    template: `
    <router-outlet></router-outlet>
    `,
    directives: [ROUTER_DIRECTIVES]
})

@RouteConfig([
  {path: '/profile/:id',   name: 'Profile', component: ProfileComponent}
])

export class AppComponent {
}

The :id is a route parameter and you can do an URL like that: /profile/2 and 2 is the value of the id parameter.

You could find more detail in the Angular2 doc : router doc

Community
  • 1
  • 1
  • 1
    Thank you, but what if I have multiple optional parameters? – K. D. Apr 18 '16 at 12:45
  • 2
    You must use queryParams in this case and there is a link on StackOverflow for that: [route optional parameters](http://stackoverflow.com/questions/34208745/angular-2-optional-route-parameter) – Stéphane Garnier Apr 18 '16 at 12:53