gpt4 book ai didi

angular - 如何在 Angular2 中使用 httpparamserializer

转载 作者:太空狗 更新时间:2023-10-29 17:31:43 25 4
gpt4 key购买 nike

我正在尝试在 Angular2 中使用 httpparamserializer。我在谷歌上搜索了很多,但示例仅适用于 angular1,如下所示。 How to inject $httpParamSerializer for use in templateUrl

在 Angular2 中使用它的语法是什么?

最佳答案

我有完全相同的问题 HERE

我的解决方案:我有一个 POST,我将数据发布到 Web 服务并序列化我的对象,这就是我的做法。它非常简单易用。

URLSearchParams

基本示例

let params = new URLSearchParams();
params.set('search', term); // the user's search value

Set Search Parameters

我的表单组件.ts

import { Headers, RequestOptions, Http, Response, URLSearchParams } from '@angular/http';


// User is done editing, serialize and POST to web service
saveEdits(): void {
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
let options = new RequestOptions({ headers: headers });

// Dynamically serialize the entire object
// *** THIS IS THE SERIALIZATION ***
let params: URLSearchParams = this.serialize(this.selectedItem);


this._http.post('http://URLtoPOSTobjTo/path', params, options)
.map(this.extractData)
.catch(this.handleError);

}

/**
* Serializes the form element so it can be passed to the back end through the url.
* The objects properties are the keys and the objects values are the values.
* ex: { "a":1, "b":2, "c":3 } would look like ?a=1&b=2&c=3
* @param {SystemSetup} obj - The system setup to be url encoded
* @returns URLSearchParams - The url encoded system setup
*/
serialize(obj: SystemSetup): URLSearchParams {
let params: URLSearchParams = new URLSearchParams();

for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var element = obj[key];

params.set(key, element);
}
}
return params;
}

关于angular - 如何在 Angular2 中使用 httpparamserializer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39858290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com