Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

javascript - 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Its not removeControls function, its removeControl function.

 this.formGroup.removeControl("prevPwd");

This will work.

Demo


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...