K. D.
K. D.

Reputation: 4219

Angular2 $location.search() equivalent (setting query params)

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?

Upvotes: 5

Views: 2805

Answers (1)

Stéphane Garnier
Stéphane Garnier

Reputation: 199

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

Upvotes: 2

Related Questions