Angular 2 FormBuilder禁用复选框选择上的字段

Jan*_*ana 10 javascript angular2-forms angular

我已经构建了角度cli项目并且有一个带复选框的表单.某些字段必须在复选框选择时禁用.

表格如下 在此输入图像描述

需要在复选框选择事件上禁用/启用密码,新密码和重新键入密码字段.

HTML

<form [formGroup]="userProfileForm" (ngSubmit)="submitForm(userProfileForm.value)">

    <div class="row">
        <div class="col">
            <div class="form-group">
                <label> {{ 'USER_PROFILE_MODEL.USER_NAME' | translate }}</label>
                <input class="form-control" type="text" [formControl]="userProfileForm.controls['userName']">
            </div>
            <div class="form-group ">
                <label>{{ 'USER_PROFILE_MODEL.DISPLAY_NAME' | translate }}</label>
                <input class="form-control" type="text" [formControl]="userProfileForm.controls['displayName']">
            </div>
            <div class="form-group ">
                <label>{{ 'USER_PROFILE_MODEL.EMAIL' | translate }}</label>
                <input class="form-control" type="text" [formControl]="userProfileForm.controls['email']">
            </div>
        </div>
        <div class="col">
            <div class="form-group ">
                <label class="checkbox-inline">
                <input type="checkbox" value="isResetPassword" name="isResetPassword" [formControl]="userProfileForm.controls['isResetPassword']">  
                 {{ 'USER_PROFILE_MODEL.RESET_PASSWORD' | translate }}
              </label>
            </div>
            <div class="form-group ">
                <label>{{ 'USER_PROFILE_MODEL.PASSWORD' | translate }}</label>
                <input class="form-control" type="password" [formControl]="userProfileForm.controls['password']">
            </div>
            <div class="form-group ">
                <label>{{ 'USER_PROFILE_MODEL.NEW_PASSWORD' | translate }}</label>
                <input class="form-control" type="password" [formControl]="userProfileForm.controls['newPassword']">
            </div>
            <div class="form-group ">
                <label>{{ 'USER_PROFILE_MODEL.RE_TYPE_PASSWORD' | translate }}</label>
                <input class="form-control" type="password" [formControl]="userProfileForm.controls['reTypePassword']">
            </div>

        </div>

    </div>
</form>
Run Code Online (Sandbox Code Playgroud)

ts代码

this.isResetPassword = true;
this.userProfileForm = formBuilder.group({
    'userName': [null, [Validators.required]],
    'displayName': [null],
    'email': [null, [Validators.required]],
    'isResetPassword': this.isResetPassword,
    'password': [{
        value: null,
        disabled: this.isResetPassword
    }],
    'newPassword': [{
        value: null,
        disabled: this.isResetPassword
    }],
    'reTypePassword': [{
        value: null,
        disabled: this.isResetPassword
    }]
})
Run Code Online (Sandbox Code Playgroud)

表单已在构造函数内部构建.如何在复选框选中禁用/启用上述字段.

dev*_*033 22

首先,我相信你想要启用字段,当且仅当选中isResetPassword 复选框时,对吗?如果是这样,我们走了:

1 - 表单的构造应该是这样的:

this.userProfileForm = this.formBuilder.group({
  // ...
  password: [{
    value: null,
    disabled: !this.isResetPassword
  }],
  newPassword: [{
    value: null,
    disabled: !this.isResetPassword
  }],
  reTypePassword: [{
    value: null,
    disabled: !this.isResetPassword
  }]
});
Run Code Online (Sandbox Code Playgroud)

请注意,这里我只在this.isResetPasswordfalse 时禁用输入.

2 - 要检测您的更改<input type="checkbox">,您可以(change)模板中使用:

<label>
  <input 
    type="checkbox" 
    [formControl]="userProfileForm.controls['isResetPassword']" 
    (change)="handleChange($event)">
  {{ 'USER_PROFILE_MODEL.RESET_PASSWORD' | translate }}
</label>
Run Code Online (Sandbox Code Playgroud)

......甚至在组件中使用valueChanges:

this.userProfileForm.get('isResetPassword').valueChanges.subscribe(value => this.handleChange(value));
Run Code Online (Sandbox Code Playgroud)

而且,当然,这是function操纵字段的状态.

handleChange(value: boolean): void {
  const passwordCtrl = this.userProfileForm.get('password');
  const newPasswordCtrl = this.userProfileForm.get('newPassword');
  const reTypePasswordCtrl = this.userProfileForm.get('reTypePassword');

  if (value) {
    passwordCtrl.enable();
    newPasswordCtrl.enable();
    reTypePasswordCtrl.enable();
  } else {
    passwordCtrl.disable();
    newPasswordCtrl.disable();
    reTypePasswordCtrl.disable();
  }
}
Run Code Online (Sandbox Code Playgroud)

一些技巧:

1 - 虽然这只是一个偏好问题,但值得一提的是你不需要[formControl]像这样使用:

[formControl]="userProfileForm.controls['isResetPassword']"
Run Code Online (Sandbox Code Playgroud)

相反,您可以简单地使用formControlName:

formControlName="isResetPassword"
Run Code Online (Sandbox Code Playgroud)

请注意,您可以对所有控件执行相同操作.

2 - 您不需要传递表单值,(ngSubmit)因为您已经引用了userProfileFormin form.

代替:

(ngSubmit)="submitForm(userProfileForm.value)"

submitForm(value: any) { console.log(value); }
Run Code Online (Sandbox Code Playgroud)

这个:

(ngSubmit)="submitForm()"

submitForm() { console.log(this.userProfileForm.value); }
Run Code Online (Sandbox Code Playgroud)

3 - 如果你想看到value禁用输入,而不是.value,你应该使用.getRawValue(),如下所示:

this.userProfileForm.getRawValue();
Run Code Online (Sandbox Code Playgroud)

演示(使用(change))

演示(使用valueChanges)


Fab*_*nes 5

最简单的方法是为密码创建一个表单组:

this.userProfileForm = formBuilder.group({
  'userName': [null, [Validators.required]],
  'displayName': [null],
  'email': [null, [Validators.required]],
  password: formBuilder.group({
    'isResetPassword': this.isResetPassword,
    'password': [null],
    'newPassword': [null],
    'reTypePassword': [null]
  })
})
Run Code Online (Sandbox Code Playgroud)

然后在模板上将密码col更改为此:

<div class="col" formGroupName="password>
  <div class="form-group ">
    <label class="checkbox-inline">
      <input type="checkbox" formControlName="isResetPassword">  
      {{ 'USER_PROFILE_MODEL.RESET_PASSWORD' | translate }}
    </label>
  </div>
  <div class="form-group ">
    <label>{{ 'USER_PROFILE_MODEL.PASSWORD' | translate }}</label>
    <input class="form-control" type="password" formControlName="password" >
  </div>
  <div class="form-group ">
    <label>{{ 'USER_PROFILE_MODEL.NEW_PASSWORD' | translate }}</label>
    <input class="form-control" type="password" formControlName="newPassword">
  </div>
  <div class="form-group ">
    <label>{{ 'USER_PROFILE_MODEL.RE_TYPE_PASSWORD' | translate }}</label>
    <input class="form-control" type="password" formControlName="reTypePassword">
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

在您的组件上:

//when value changes, to disable all the fields just do this
this.userProfileForm.controls.password.disable();
// to enable them back
this.userProfileForm.controls.password.enable();
Run Code Online (Sandbox Code Playgroud)