Is there anything like this $.param() function from JQuery for Angular2?
I know Angular 1 specifically has a service like it, Angular1 Equivalent
I have checked the Angular 2 site here and they have a demo of a POST but they send a JSON in the body of the request.
My current, non working, attempt
saveEdits() {
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
let options = new RequestOptions({ headers: headers });
this._http.post("ajax/update-thingy", JSON.stringify(this.dataJson), options)
.map(this.extractData)
.toPromise()
.catch(this.handleError);
// update in Angular 1 using JQuery function
// $http({
// method : 'POST',
// url : 'ajax/update-thingy',
// data : $.param($scope.formData),
// headers: {'Content-Type': 'application/x-www-form-urlencoded'}
// });
}
extractData(res: Response) {
let body = res.json();
if (body === 'failed') {
body = [];
}
return body || [];
}
private handleError(error: any) {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return Promise.reject(errMsg);
}
Or if it is easier to get JQuery in my Angular2 maybe that is a way to go.
UPDATE
I did solve my own issue. Please see my answer below!
I solved my own issue.
Here is what I did, I used URLSearchParams from Angular 2. It has worked out pretty well. Not at all complicated and easy to use.
import { Headers, RequestOptions, Http, URLSearchParams } from '@angular/http';
saveEdits() {
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
let options = new RequestOptions({ headers: headers });
let params: URLSearchParams = this.serialize(this.myObj);
this._http.post("ajax/update-thingy", params, options)
.map(this.extractData)
.toPromise()
.catch(this.handleError);
this.selectedBill = null;
}
serialize(obj: any) {
let params: URLSearchParams = new URLSearchParams();
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var element = obj[key];
params.set(key, element);
}
}
return params;
}
There is a built in serialize function, but its not exported.. So we need to go a little workaround..
You could do it like this: https://plnkr.co/edit/ffoaMVbwSOIX5YNLo2ip?p=preview
import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { DefaultUrlSerializer, UrlSegment, UrlTree } from '@angular/router';
@Component({
selector: 'my-app',
template: `
<div>
<h2>{{way1}}</h2>
<h2>{{way2}}</h2>
</div>
`,
})
export class App {
private way1: string;
private way2: string;
constructor() {
let myParams = {you: 'params', will: 'be', here: '!!'};
// way 1
let serializer = new DefaultUrlSerializer();
let nackedUrlTree = serializer.parse('');
nackedUrlTree.queryParams = myParams;
this.way1 = 'way1: ' + serializer.serialize(nackedUrlTree);
// way 2
let urlSeg = new UrlSegment('', myParams);
this.way2 = 'way2: ' + urlSeg;
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With