简体   繁体   中英

How to remove the field 'prevPwd' from the formbuilder in angular

Here's the code:

TS

user = true;

this.fb.group({
      password: ['', [
        Validators.required,
        this.passwordValidator
      ]],
      newPassword: ['', [Validators.required]],
      prevPwd: ['',
        Validators.required
      ]
    });

What I'm trying to do here is to remove the prevPwd or previous password field, when the user is true, but when user is false it should be required the field prevPwd .

I already tried.

this.formGroup = this.fb.group({
          password: ['', [
            Validators.required,
            this.passwordValidator
          ]],
          newPassword: ['', [Validators.required]],
          prevPwd: ['',
            Validators.required
          ]
        });
if (user) {
 this.formGroup.removeControl('prevPwd');
}

return this.formGroup

But it doesn't work.

Its not removeControls function, its removeControl function.

 this.formGroup.removeControl("prevPwd");

This will work.

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM