gpt4 book ai didi

angular - 当表单无效时禁用提交表单 Angular + Material

转载 作者:行者123 更新时间:2023-12-02 03:40:47 25 4
gpt4 key购买 nike

我正在使用 Angular 5.1.1/Typescript 2.4.2 + Material 5 和 sat-popover 构建表单图书馆

尝试在输入无效时禁用提交按钮,但没有成功。我正在使用 Material Input validation example

验证有效,我收到一条错误消息,但按钮未禁用,并且提交的表单为空值。我不知道如何在输入无效时使用正确的条件来禁用按钮。已经尝试过

ng-disabled="!flagForm.valid"

ng-disabled="flagForm.$invalid"

使用时

[disabled]="!flagForm.valid"

我收到“类型错误:无法读取未定义的属性“有效””

它们似乎都不起作用。我缺少什么?这是完整的代码。

   import { Component, Input, Optional, Host } from '@angular/core';
import { FormControl, FormGroupDirective, NgForm, Validators} from '@angular/forms';
import { SatPopover } from '@ncstate/sat-popover';
import { filter } from 'rxjs/operators/filter';
import { ErrorStateMatcher} from '@angular/material/core';

/** Error when invalid control is dirty, touched, or submitted. */
export class MyErrorStateMatcher implements ErrorStateMatcher {
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
const isSubmitted = form && form.submitted;
return !!(control && control.invalid && (control.dirty || control.touched || isSubmitted));
}
}

@Component({
selector: 'inline-edit',
styleUrls: ['inline-edit.component.scss'],
template: `
<form (ngSubmit)="onSubmit()" name="flagForm" novalidate>
<div class="mat-subheading-2">Submit Your flag</div>

<mat-form-field>
<input matInput maxLength="140" name="flag" [(ngModel)]="flag" [formControl]="flagFormControl"
[errorStateMatcher]="matcher">
<mat-hint align="end">{{flag?.length || 0}}/140</mat-hint>
<mat-error *ngIf="flagFormControl.hasError('required')">
Flag is <strong>required</strong>
</mat-error>
</mat-form-field>

<div class="actions">
<button mat-button type="button" color="primary" (click)="onCancel()" class="btn btn-secondary m-btn m-btn--air m-btn--custom">CANCEL</button>
<button mat-button type="submit" ng-disabled="!flagForm.valid" color="primary" class="btn btn-accent m-btn m-btn--air m-btn--custom">Submit</button>
</div>
</form>
`
})
export class InlineEditComponent {

flagFormControl = new FormControl('', [
Validators.required
]);

matcher = new MyErrorStateMatcher();

/** Overrides the flag and provides a reset value when changes are cancelled. */
@Input()
get value(): string { return this._value; }
set value(x: string) {
this.flag = this._value = x;
}
private _value = '';

/** Form model for the input. */
flag = '';

constructor(@Optional() @Host() public popover: SatPopover) { }

ngOnInit() {
// subscribe to cancellations and reset form value
if (this.popover) {
this.popover.closed.pipe(filter(val => val == null))
.subscribe(() => this.flag = this.value || '');
}
}

onSubmit() {
if (this.popover) {
this.popover.close(this.flag);
}
}

onCancel() {
if (this.popover) {
this.popover.close();
}
}
}

最佳答案

有必要在类中实例化一个表单,如下所示

flagForm: FormGroup;

关于angular - 当表单无效时禁用提交表单 Angular + Material,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48706666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com