【问题标题】:The disable method does not work on the Reactive Formdisable 方法不适用于响应式表单
【发布时间】:2020-06-27 02:50:31
【问题描述】:

我试图通过一个条件来禁用复选框,但它没有被禁用。

我强行在 oninit 中插入了该方法,还签入了console.log() 表单,它有status: "DISABLE" 但复选框没有被禁用,如果我在复选框中插入禁用的数据绑定,角度会抱怨,说我使用的是响应式表单,我需要使用 .disabled() 方法。

合同表格组件

 @Component({
      selector: 'app-contract-form',
      template: `
        <app-form [class]="'flex__display align__items__center bg__color__red__light'" [fmGroup]="contractForm">
          <app-input
            (onclick)="onSubmitForm()"
            [class]="'cursor__pointer height__rem__2 width__rem__2 margin__xy__2'"
            [fmGroup]="contractForm"
            [fmControlName]="'termsAndConditions'"
            [type]="'checkbox'"
            [checked]="formValue"
          >
          </app-input>
    
          <app-span>
            Declaro que conheço e concordo com as
            <app-span
              (onclick)="clauseDocumentClick.emit(true)"
              [class]="'cursor__pointer text__color__blue__dark text__decoration__underline'"
            >
              Cláusulas Gerais do Contrato.
            </app-span>
          </app-span>
        </app-form>
      `,
    })
    export class ContractFormComponent implements OnInit {
      contractForm!: FormGroup;
      @Output() submitForm = new EventEmitter<boolean>();
      @Output() clauseDocumentClick = new EventEmitter<boolean>();
      @Input() protected set setDisabledForm(isDisabled: boolean) {
        this.contractForm.controls.termsAndConditions[isDisabled ? 'disable' : 'enable']();
      }
    
      constructor(protected readonly formBuilder: FormBuilder) {
        this.contractForm = formBuilder.group({
          termsAndConditions: formBuilder.control(true, Validators.requiredTrue)
        });
      }
    
      ngOnInit() {
        this.contractForm.disable()
        console.log(this.contractForm)
      }
    
      get formValue(): boolean {
        return this.contractForm.controls.termsAndConditions.value;
      }
    
      onSubmitForm(): void {
        this.contractForm.controls.termsAndConditions.patchValue(!this.formValue);
        this.submitForm.emit(this.formValue);
      }
    }

表单组件

@Component({
  selector: 'app-form',
  template: `
    <form [formGroup]="fmGroup" [class]="class" [autocomplete]="autocomplete" novalidate>
      <ng-content></ng-content>
    </form>
  `
})
export class FormComponent implements OnInit {
  @Input() fmGroup!: FormGroup;
  @Input() autocomplete = 'on';
  @Input() colectionText: string;

  ngOnInit() {}

}

输入组件

@Component({
  selector: 'app-input',
  template: `
  <input
  (click)="onclick.emit($event)"
  [formControlName]="fmControlName"
  [class]="class"
  [type]="type"
  [placeholder]="placeholder"
  [autocomplete]="autocomplete"
  [minLength]="minlength"
  [maxLength]="maxlength"
  [readonly]="readonly"
  [autofocus]="autofocus"
  [checked]="checked"
  [required]="required"
 >
  `,
})
export class InputComponent implements OnInit {
  @Output() onclick = new EventEmitter<MouseEvent>();
  @Input() fmControlName!: FormControlName;
  @Input() class = '';
  @Input() type = 'text';
  @Input() placeholder = '';
  @Input() autocomplete = 'on';
  @Input() minlength = 1;
  @Input() maxlength = 524288;
  @Input() readonly = false;
  @Input() autofocus = false;
  @Input() checked = false;
  @Input() required = true;

  constructor() {}

  ngOnInit() {}
}

【问题讨论】:

  • 您可以单独禁用条款和条件而不是整个表单吗? ngOnInit...this.contractForm.get('termsAndConditions').disable()

标签: angular angular-reactive-forms


【解决方案1】:

当您在ngOnInit() 中禁用整个表单时,它应该禁用表单内的所有控件,包括复选框。

检查 stackblitz - https://stackblitz.com/edit/angular-reactive-forms

如果您想根据某些条件禁用复选框等特定控件,则可以使用this.contractForm.get('termsAndConditions').disable()

【讨论】:

  • 我明白这一点,但他并没有禁用该复选框。如果我没有传递 formControlName,而是将 formControl 传递给输入组件,它只会禁用复选框。但是,如果我这样做,它会出错并且不会更改复选框值。
  • 能否分享一下 组件。您应该使用 formControlName = 'termsAndConditions' 绑定控件
  • 我贴了三个组件。
  • 这似乎是 InputComponent 中的拼写错误。有 '!'在 formControlName 之后。 @Input() fmControlName!:FormControlName。与 @Input() fmGroup! 相同:FormComponent 中的 FormGroup。
  • 没有错字。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-28
  • 2020-02-06
  • 2014-10-31
  • 2021-11-17
  • 2021-11-08
  • 1970-01-01
  • 2017-11-21
相关资源
最近更新 更多