当我通过以下方式以 react 形式获取值时有什么区别:
this.someForm.controls['firstName'].value
this.someForm.get('firstName').value
public someForm: FormGroup = this.formBuilder.group({
firstName: ['', Validators.required],
});
this.someForm.controls['firstName'].value
this.someForm.get('firstName').value<br>
以上是我的表单以及从表单中获取值的两种不同方式。但是如果有的话,黑白两种方式有什么区别?
最佳答案
这两者之间没有太多区别,但是
Angular 团队可能会选择更改 FormGroup
结构在 future ,如果我们使用 controls
直接属性,它可能会导致破坏性更改,而 get()
方法可以调整以反射(reflect)新的结构。
FormGroup.get is designed to access target formcontrol by it's path, which makes it easy to get the target control from multi layer embed form and also makes code clear and easily to understand.
关于此的信息不多,但您可以在此处阅读非常相似的内容,
关于Angular Reactive Forms 控件与 Get,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57411570/