角度反应形式:FormControl -> get() 方法有什么意义吗?



在角反应性形式中, FormControlAbstractControl继承get()方法。然后调用_find()方法,其中参数 controlFormControl

function _find(control: AbstractControl, path: Array<string|number>| string, delimiter: string) {
  if (path == null) return null;
  if (!(path instanceof Array)) {
    path = (<string>path).split(delimiter);
  }
  if (path instanceof Array && (path.length === 0)) return null;
  return (<Array<string|number>>path).reduce((v: AbstractControl, name) => {
    if (v instanceof FormGroup) {
      return v.controls.hasOwnProperty(name as string) ? v.controls[name] : null;
    }
    if (v instanceof FormArray) {
      return v.at(<number>name) || null;
    }
    return null;
  }, control);
}

我们可以看到,此方法仅在FormGroupFormArray中搜索path(!)。

那么,formControl-> get()方法是否有任何感觉?我只是不知道需要此方法的情况吗?

可能没有必要。我找不到任何控制逻辑。由于组,数组和控制所有从AbstractControl中获得的方法,因此GET方法出现在Control中。

您有两种在formgroup中获得控件的方法

myForm.controls.name_of_control
//and
myForm.get('name_of_control')

使用" get"确保如果角更改formGroup(*)的"模型",则此更改不影响您的代码

(*)想象一下,在较新版本的Angular中,控件存储在一个名为" constureControls"的属性中,但是您会失去myform.controls,因为此属性具有消失

最新更新