I want to send query parameters within a GET request. My class looks like this:
@Injectable()
export class Loader implements TranslateLoader{
constructor(private http: Http){
}
getTranslation(lang: string): Observable<any>
{
return this.http.get(routes.Localization.Get) ;// in that place I need to pass params
}
}
How can I do this?
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"];
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.