[英]Angular 2 - Object Map with custom key fields?
I have a list in my Angular app that I am trying to POST with. 我的Angular应用程序中有一个列表,我想用它发布。 The user is able to enter in their own key-value pair.
用户可以输入自己的键值对。 So I have two inputs like:
所以我有两个输入,例如:
<input type="text" id="name" [(ngModel)]="name" />
<input type="text" id="value" [(ngModel)]="value" />
<button class="btn" (click)="addToList()" />
And my TS file would have something like: 我的TS文件中的内容如下:
myList : any = [];
name = "";
value = "";
addToList() {
//I wanted to do myList.push({this.name:this.value}), but it didn't work
myList[this.name]=this.value;
this.name = "";
this.value = "";
}
onSubmit() {
var submitJSON = {
userList = myList,
....
}
console.log(submitJSON);
}
However, in my submit flow, it shows the object as empty, and the length as zero. 但是,在我的提交流程中,它显示对象为空,长度为零。 When I use console commands, I can see the list is populated at the time I construct my submit object, but it isn't added to my submit object, and the length is 0. When I inspect in the console, I can see my object, I can see it has a key-value pair in the list, but the length is 0. Am I doing something wrong?
使用控制台命令时,可以看到在构造提交对象时填充了该列表,但是列表未添加到提交对象中,并且长度为0。在控制台中进行检查时,可以看到我的列表。对象,我可以看到它在列表中有一个键值对,但长度为0。我做错了吗? Should I not be pushing to my list in the way that I am?
我是否应该以自己的方式推向清单?
You need to make a minor change 您需要做些小改动
addToList() {
myList.push({[this.name]:this.value});
this.name = "";
this.value = "";
}
用这个:
myList.push({[this.name] : this.value});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.