从表单中添加数据以排列角度2+

时间:2019-05-24 12:47:00

标签: arrays angular typescript angular-reactive-forms

这是我的表格

enter image description here

输入问题时,我可以根据需要添加任意多个替代项。 实际上,将是编辑替代项的按钮。

我希望我的数组具有以下格式:

val cnt = df.select($"ID").distinct().count()
df.groupBy($"Value")
  .agg(countDistinct("ID") as "cnt")
  .filter($"cnt" === cnt)
  .select($"Value")
  .show()

当您点击添加按钮时,您可以添加多个替代项,没有限制数

以下代码使替代方法出现:

+-----+
|Value|
+-----+
|  XXX|
+-----+

当我单击“保存”以完成问题时,如何以这种方式填充数组?

1 个答案:

答案 0 :(得分:1)

我认为您的“替代方案”字段有点错误,我认为应该是这样的:

"Alternatives: [{'test':'testAnswer'},{'test2':'test2Answer'}... ]

然后,您也可以使用* ngFor迭代其他选择

我会做这样的事情:

let form = {
"Question": "Question test",
"cod" : "pw3"
"Value" : "10"
"Alternatives": [
  {
  "test": "test",
  "test2": "test",
  "test3": "test"
  },
 ]
};

然后,我将构造一个函数来添加/删除内部数组中的项目,也许是这样的:

public addOrRemoveItemInArray(a:boolean) {
// if a=true, add if false = remove
if (a === true) { 
let newAnswer = {"test": ""};
this.form.Alternatives.push(newAnswer);

  }
if (a=== false) { this.form.Alternatives.pop() //remove the las item
  }
else {}//just to avoid problems
}

当然,您应该更新html以遍历“ form”新变量

相关问题