【问题标题】:Disable a field in angular reactive form禁用角反应形式的字段
【发布时间】:2019-05-25 16:25:33
【问题描述】:

在我的 Angular 6 应用程序中,我正在制作一个反应形式,

HTML

<form [formGroup]="form">

    <h2>Click the add button below</h2>
    <button (click)="addCreds()">Add</button>
    <div formArrayName="credentials" >
      <div *ngFor="let creds of form.get('credentials').controls; let i = index" 
          [formGroupName]="i" style="display: flex">
          <select formControlName="action">
             <option *ngFor="let option of options" value="{{option.key}}">
               {{option.value}} 
             </option>
          </select>
          <input placeholder="Name" formControlName="name">
<div *ngIf="creds.value.action=='set' ||
      creds.value.action=='go'">
                   <input placeholder="Label" formControlName="label">
          </div>
      </div>
    </div>
    </form>

这里我用过

<div *ngIf="creds.value.action=='set' || creds.value.action=='go'"> <input placeholder="Label" formControlName="label"> </div>

如果条件为真,将显示字段label,否则将不显示。

但我只需要禁用该字段并且不应该完全删除它..

我已经尝试过了,

<input [disabled]="creds.value.action != 'set' || creds.value.action != 'go' " placeholder="Label" formControlName="label">

但它不起作用。

Stackblitz 与 *ngIf https://stackblitz.com/edit/angular-form-array-example-25y1ix

Stackblitz 已禁用 https://stackblitz.com/edit/angular-form-array-example-laftgu

如果选定的action(第一个下拉菜单)值为wait,请帮助我如何禁用label 字段。

【问题讨论】:

标签: javascript angular typescript angular6 angular-reactive-forms


【解决方案1】:

只需创建一个 CSS 类:

.disabled {
  pointer-events:none;
  background-color: #D3D3D3;
}

为你的组件添加一个方法:

getValueByIndex(index) {
  const actionValue = ( < FormArray > this.form.get('credentials')).at(index).value.action;
  return actionValue !== 'set' && actionValue !== 'go';
}

在你的模板中使用这个方法:

<input 
    [class.disabled]="getValueByIndex(i)" 
    placeholder="Label" 
    formControlName="label">

这里有一个Working Sample StackBlitz 供您参考。


更新:

或者,您可以执行以下操作:

默认禁用label字段:

addCreds() {
  const creds = this.form.controls.credentials as FormArray;
  creds.push(this.fb.group({
    action: '',
    name: '',
    label: {
      value: '',
      disabled: true
    },
    state: 'na'
  }));
}

然后在表单上监听(ngModelChange)事件更新标签字段的状态:

<select 
  formControlName="action"
  (ngModelChange)="($event === 'set' || $event === 'go') ? creds.controls.label.enable() : creds.controls.label.disable()">
  <option 
    *ngFor="let option of data" 
    [value]="option.key">
    {{option.value}} 
  </option>
</select>

这是此方法的Working Sample StackBlitz。


感谢 A.Winnen 就使用前一种方法对性能的影响提出了 cmets。

【讨论】:

  • 如果我不更改下拉列表但有一些默认值,解决方案是什么?说在行动中,我将默认值设置为set,然后需要启用相应的label 字段。因为此解决方案仅适用于ngModelChange,但我需要更改的结果以及是否存在默认值修补到action..
【解决方案2】:

在 Reactive Form 中,您不能使用 disabled 指令。相反,您需要通过调用 enable() 或 disable() 函数以“反应方式”禁用 formControl。

addCreds() {
    const creds = this.form.controls.credentials as FormArray;
    const newGroup = this.fb.group({
      action: '',
      name: '',
      label: ''
    });
    newGroup.controls.action.valueChanges.subscribe((currentAction) => {
      if (typeof currentAction === "string" && currentAction.toLowerCase() === 'wait') {
        newGroup.controls.label.disable();
      } else {
        newGroup.controls.label.enable();
      }
    });
    creds.push(newGroup);
  }

我修改了你的 stackblitz 示例:https://stackblitz.com/edit/angular-form-array-example-ymysw4

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-07
    • 2019-04-21
    • 2022-11-03
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多