设置Formgroup有效或无效角度7

Raf*_*ura 5 angular

我有一种表单,需要将字段设置为有效或无效,因为我想编辑用户,但按钮保持禁用状态

编辑组件

ngOnInit() {
  this.getForm();
  this.getRole();
  console.log(this.formGroup.valid)
}

getForm() {
 this.formGroup = this.formBuilder.group({
   name: ['', Validators.required],
   label: ['', Validators.required],
  });
}
Run Code Online (Sandbox Code Playgroud)

当我进入页面编辑时,我希望能够将按钮设置为无效表单

<button type="submit" mat-button color="primary" [disabled]="!formGroup.valid">Alterar</button>
Run Code Online (Sandbox Code Playgroud)

例如

if(user) {
  this.formGroup (invalid)
 } else {
  this.formGroup (valid)
}  
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这是我编辑项目的页面,但即使填写了字段,表单组也无效,为什么?

Iam*_*oud 0

你在 HTML 中所做的事情是正确的。也许将其设置为 更好[disabled]="formGroup.invalid"

我不明白你的 if 语句示例。你到底想问什么?