Angular 2 http.get with params
You can leverage the URLSearchParams
class for this:
getTranslation(lang: string): Observable<any> { let params = new URLSearchParams(); params.set('param1', 'value1'); return this.http.get(routes.Localization.Get, { search: params });}
This will result to an URL like this (parameters are added in the query string): http://...?param1=value1
.
See the documentation of this class:
It now providers support for encoding / decoding parameters.
This is pretty simple - you can define your URLSearchParams
and pass them in the second parameter of the http.get
method:
import { URLSearchParams } from '@angular/http'let params: URLSearchParams = new URLSearchParams();params.set('param1', 'someValue');params.set('param2', 'someValue');return this.http.get(routes.Localization.Get, { search: params });
When an URL like this http://stackoverflow.com?param1=value
You can get the param1 by code follow:
import { Component, OnInit } from '@angular/core';import { Router, ActivatedRoute, Params } from '@angular/router';@Component({ selector: '', templateUrl: './abc.html', styleUrls: ['./abc.less']})export class AbcComponent implements OnInit { constructor(private route: ActivatedRoute) { } ngOnInit() { // get param let param1 = this.route.snapshot.queryParams["param1"]; }}