2017-07-11 50 views
2

利用无表格,我已经创造了一些控制角4 - 重置反应形式

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'), 
     }) 
    }) 
} 

“名”和“城市”呈现为一个文本字段(带有默认值城市)和“结构'呈现为复选框(全部被选中)。

现在,我需要提供一个选项来将窗体重置为默认值。所以,点击重置按钮,我执行以下代码

onReset() { 
    //Leaving out 'name', as it does not have a default value 
    this.myForm.reset({ 
     'city': 'London', 
     'structure': ?????, //What should I do here 
    });  
} 

form.reset重置表单。不过,我还需要恢复默认值。恢复“城市”的默认值很容易,因为它只是一个表单控件。但是,如何重置“结构”(它是FormGroup)内的3个复选框的值?

回答

4
'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, 
});  
}