Angular 4 - 重置反应表单

时间:2017-07-11 18:06:14

标签: angular

使用Reactive Forms,我创建了一些控件

url1 = "xxx.autotrader.com"

data1 = requests.get(url1)

searchKey = 'Toyota'
searchEndKey = '='

textIwant = data1.text[data1.text.find(searchKey)+len(searchKey):data1.text.find(searchEndKey,data1.text.find(searchKey)+len(searchKey)+1)]

textIwant needs to = 12345 
if not, go back to data1 to read the site again
if textIwant now = 12345 then continue with script, if not, return to data1 to try again to max tries = 3

'名称'和' city'被渲染为文本字段(城市具有默认值)和'结构'被渲染为复选框(所有这些都被选中)。

现在,我需要提供一个选项,将表单重置为默认值。因此,单击“重置”按钮,我将执行以下代码

 ngOnInit() {
    this.myForm = new FormGroup({
      'name': new FormControl(null),
      'city': new FormControl('London'),
      'structure': new FormGroup({
        'Parallel': new FormControl('Parallel'),
        'Hierarchical': new FormControl('Hierarchical'),
        'Stable': new FormControl('Stable'),
      })
    })
}

form.reset重置表单。但是,我还需要恢复默认值。恢复' city'的默认值很简单,因为它只是一个表单控件。但是,如何重置'结构'内的3个复选框的值。 (这是一个FormGroup)?

1 个答案:

答案 0 :(得分:8)

'structure' is a key-value pair by itself:
 onReset() {
   //Leaving out 'name', as it does not have a default value
    this.myForm.reset({
      'city': 'London',
      'structure':{
        'Parallel': 1,
        'Hierarchical': 1,
        'Stable': 1,
 });     
}