用户打开模态,并且在填写所需的输入之前不能单击确定
我尝试了documentation中提到的内容,但没有用
这里是working Demo stackblitz,需要实现这一点
component.ts
import { Component, Inject } from '@angular/core';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
export interface DialogData {
animal: string;
name: string;
}
/**
* @title Dialog Overview
*/
@Component({
selector: 'dialog-overview-example',
template: `<button mat-raised-button (click)="openDialog()">Pick one</button>
<li *ngIf="animal">
You chose: <i>{{animal}}</i>
</li>`
})
export class DialogOverviewExample {
animal: string;
name: string;
constructor(public dialog: MatDialog) { }
openDialog(): void {
const dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
width: '250px',
data: { name: this.name, animal: this.animal }
});
dialogRef.afterClosed().subscribe(result => {
console.log('The dialog was closed');
this.animal = result;
});
}
}
@Component({
selector: 'dialog-overview-example-dialog',
templateUrl: 'dialog-overview-example-dialog.html',
})
export class DialogOverviewExampleDialog {
constructor(
public dialogRef: MatDialogRef<DialogOverviewExampleDialog>,
@Inject(MAT_DIALOG_DATA) public data: DialogData) { }
onNoClick(): void {
this.dialogRef.close();
}
}
/** Copyright 2019 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license */
dialog.html
<h1 mat-dialog-title>Hi {{data.name}}</h1>
<div mat-dialog-content>
<p>What's your favorite animal?</p>
<mat-form-field>
<input required matInput name="myInput" [(ngModel)]="data.animal">
</mat-form-field>
</div>
<div mat-dialog-actions>
<button mat-button (click)="onNoClick()">No Thanks</button>
<!-- <button mat-button [disabled]="myInput.errors.required" [mat-dialog-close]="data.animal" cdkFocusInitial>Ok</button> -->
<button mat-button [mat-dialog-close]="data.animal" cdkFocusInitial>Ok</button>
</div>
答案 0 :(得分:1)
当用户不填写任何数据时,仅禁用“确定”按钮怎么办?像这样:
<button mat-button [disabled]="!data.animal" [mat-dialog-close]="data.animal" cdkFocusInitial>Ok</button>
您还可以将输入内容包装在<form #form="ngForm">...</form>
标记内,并检查表单是否有效[disabled]="form.invalid"