arrays - Angular - 模板驱动形式 - 以数组中对象的形式发送数组数据
问题描述
我在多选下拉列表中显示了一些列表。提交的数据在表单中
[ "object1" , "object2" ]
我希望它以以下形式提交:
[ { object 1 } , { object 2} ]
我的 html 下拉列表代码:
<select name="service" multiple matNativeControl required ngModel>
<option *ngFor="let service of subCategoriesList" value="{{service.id}}">{{service.service}}</option>
</select>
组件.ts 文件:
addPackage(form: NgForm){
const body = {
package_name : form.value.name,
services: form.value.service, //the value required in objects in an array
}
console.log("add package body is: ", body)
任何帮助将不胜感激。
解决方案
尝试使用Object.assign
;
在 addPackage 函数中:然后为每个选择调用创建一个空数组Object.assign
addPackage(form: NgForm){
let selections=[];
form.value.service.forEach(element => {
var selection= Object.assign({}, { element });
selections.push(selection);
});
const body = {
package_name : form.value.name,
services: selections, //put your created objects here
}
console.log("add package body is: ", body)
}
推荐阅读
- android - 我该如何实现这个屏幕设计?需要屏幕兼容性
- c# - MSBUILD 抱怨空条件运算符
- c# - Azure 静态文件 System.IO.DirectoryNotFoundException
- powershell - 如何在powershell中从export-csv中删除双引号
- c# - 如何从 c#.net 执行 Python 脚本文件
- c# - 如何将 JSON 发送到 C# Web 服务
- sql - 如何使用链在oracle sql developer中依次执行该过程?
- python - Swig:在 Python 中使用 c++ STL 复杂
- python - Python - 父母和多线程问题
- python - 在 RStudio 上运行 Python 代码(通过 Flexdashboard)