简体   繁体   English

如何从 angular 的表单生成器中删除字段“prevPwd”

[英]How to remove the field 'prevPwd' from the formbuilder in angular

Here's the code:这是代码:

TS 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 .我在这里尝试做的是删除prevPwd或以前的密码字段,当用户为真时,但当用户为假时,应该需要字段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.它不是removeControls function,它的removeControl function。

 this.formGroup.removeControl("prevPwd");

This will work.这将起作用。

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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