Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 361
K. D. Avatar asked Oct 30 '22 05:10

K. D.


1 Answers

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

like image 185
Stéphane Garnier Avatar answered Nov 15 '22 06:11

Stéphane Garnier