【发布时间】:2018-06-15 02:53:57
【问题描述】:
所以我有一些可选的输入字段,我需要构建一个 json 对象,然后将其发送到 http.post。可选意味着如果字段为空,那么我也不会将其添加到 json 属性中。这是以下输入字段。
<input type="text" [(ngModel)]="searchQuery" placeholder="Keyword">
<div class="row">
<div class="col-lg-6">
<p-calendar id="kera" [(ngModel)]="startDate" dateFormat="yy-mm-dd" placeholder="Start date" [showIcon]="true"></p-calendar>
</div>
<div class="col-lg-6">
<p-calendar id="kera" [(ngModel)]="endDate" dateFormat="yy-mm-dd" placeholder="End date" [showIcon]="true"></p-calendar>
</div>
</div>
将发送到 http 的预期对象应如下所示:
"search": {
"scope": [2, 3, 32],
"type": "basic",
"text": {
"value": searchQuery, //string variable coming from UI
},
"date": {
"type": "range",
"from": startDate, //string variable coming from UI
"to": endDate //string variable coming from UI
}
}
是否应该使用 json.prase 来完成?或者应该是类似这样的东西,
var search = {};
search.text = { value: "", fields: [] };
{value: "", fields: Array(0)}
seach.text.value = "tom";
search.text.value = "tom";
search.text.fields.push("subject");
search.text.fields.push("body");
所以我必须在发送之前手动构建对象
【问题讨论】:
-
您不必将其解析为 json 只需将其作为对象发送
-
但是我必须在发送之前手动构建它,如果字段为空我不发送属性,不知道如何实现这个
标签: javascript json angular rest api