相关疑难解决方法(0)

formGroup.get vs formGroup.controls in reactive形式 - Angular

使用时选择验证是否有任何首选方法

  • myForm.controls['name'].valid
  • myForm.get('name').valid

因为两者似乎只是在语法上不同,但实现了相同的目标.

<label>Name
  <input type="text" formControlName="name">
</label>
<div class="alert" *ngIf="!myForm.controls['name'].valid && myForm.controls['name'].touched">
  {{ titleAlert }}
</div>
Run Code Online (Sandbox Code Playgroud)

与...一样

<div class="alert" *ngIf="!myForm.get('name').valid && myForm.get('name').touched">
  {{ titleAlert }}
</div>
Run Code Online (Sandbox Code Playgroud)

从我在代码中检查,get有这个代码:

AbstractControl.prototype.get = function (path) { return _find(this, path, '.'); };
Run Code Online (Sandbox Code Playgroud)

我刚刚开始使用Angular,所以我们将非常感谢专家意见.

angular angular-reactive-forms

25
推荐指数
2
解决办法
1万
查看次数

标签 统计

angular ×1

angular-reactive-forms ×1