代码之家  ›  专栏  ›  技术社区  ›  Maniraj Murugan

禁用角度反应形式的字段

  •  1
  • Maniraj Murugan  · 技术社区  · 6 年前

    在我的角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

    2 回复  |  直到 6 年前
        1
  •  3
  •   SiddAjmera    6 年前

    只需创建一个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对使用前一种方法的性能影响的评论。

        2
  •  0
  •   A.Winnen    6 年前

    在反应形式中,不能使用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