Angular Reactive Forms 控件与 Get

标签 angular angular-forms angular-formbuilder

当我通过以下方式以 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.


关于此的信息不多,但您可以在此处阅读非常相似的内容,
  • formGroup.get vs formGroup.controls in reactive form - Angular
  • angular-reactive-forms-tips
  • 关于Angular Reactive Forms 控件与 Get,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57411570/

    相关文章:

    Angular - 动态添加/删除验证器

    angular - 无法使用具有选择值的 patchValue 绑定(bind)到对象

    Angular 5 : Dynamic Form Validation inside a table

    javascript - Angular 2 Resolve - 局部变量范围

    angular - useAsDefault 不能与 Electron + Webpack 实现一起使用? ( Angular 2)

    angular - 如何使用 Angular2 创建带有图形/图表的 pdf 文件?

    带有单选按钮的 Angular react 表单数组

    javascript - 隐藏没有 li 子元素的父元素

    forms - 形式中的形式。表单控件可以继承吗?

    javascript - 使用 Angular 7 中的单个提交按钮创建具有动态表单字段的动态表单