按回车键时,Angular6 材质对话框防止关闭

bbu*_*ver 2 dialog angular6 angular-material-6

我有一个登录对话框,并希望在按下 Enter 时防止它自动关闭。

更具体地说,当用户输入凭据并按下 Enter 并且凭据响应作为错误返回时,我希望对话框保持不变(因此我可以向他们显示一些错误消息并让用户再次尝试)。

所以这就是我所做的:

export class LoginComponent {
    constructor(public dialogRef: MatDialogRef<LoginComponent>) { }

    onSubmit(): void {  
        this.authService.login(...)
            .subscribe(res => { 
                ... 
             },
            error => {
                this.dialogRef.disableClose = true;
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

this.dialogRef.disableClose = true; 即使响应作为错误返回,仍然关闭对话框。

我该怎么做?

编辑

登录.component.ts

<mat-toolbar>
    <span>Login</span>
</mat-toolbar>
<mat-card class="my-card">
    <div *ngIf="error" style="color: red;">{{error}}</div><br />
    <form (ngSubmit)="onSubmit()" [formGroup]="loginForm">     
        <mat-card-content>       
            <mat-form-field appearance="outline" class="full-width">
                <mat-label>Email</mat-label>
                <input matInput placeholder="Email" 
                   formControlName="email" 
                   [formControl]="emailFormControl" 
                   [errorStateMatcher]="matcher" />
                <mat-error *ngIf="emailFormControl.hasError('email') && !emailFormControl.hasError('required')">
                    Enter valid email address
                </mat-error>   
                <mat-error *ngIf="emailFormControl.hasError('required')">
                    Required field
                </mat-error>
            </mat-form-field>
            <mat-form-field appearance="outline" class="full-width">
                <mat-label>Password</mat-label>
                <input matInput type="password" 
                   placeholder="Password" 
                   formControlName="password" 
                   [formControl]="passwordFormControl" 
                   [errorStateMatcher]="matcher" />
                <mat-error *ngIf="passwordFormControl.hasError('required')">
                    Required field
                </mat-error>
            </mat-form-field>                
        </mat-card-content>
        <mat-card-actions>
            <button mat-raised-button (click)="onNoClick()" color="primary">Close</button>
            <button mat-raised-button 
                [disabled]="!(loginForm.controls.email.valid && loginForm.controls.password.valid)" 
                color="accent">
                Login
            </button>
        </mat-card-actions>
    </form>
</mat-card>
Run Code Online (Sandbox Code Playgroud)

登录.component.ts

onSubmit(): void {       
    if (this.loginForm.invalid) {
        return;
    }             
    this.authService.login(this.loginForm.controls.email.value, this.loginForm.controls.password.value)
        .subscribe(res => {               
            if (res) {
                alert("logged in");              
            }   
        },
        error => {                  
            this.error = 'Error! Plese try again.'; 
        }
    );       
}
Run Code Online (Sandbox Code Playgroud)

小智 11

我相信这是因为你的“关闭”按钮没有设置为 type="button",我认为这是第一个具有焦点的元素,所以当按下 Enter 时,你正在输入那个按钮,默认情况下,它将提交表单. 添加 type="button" 应该可以解决它。

另外记录一下,最新版angular material有md-button默认自动添加type="button"(除非你指定type="submit")来避免这种情况

用这个

 <mat-card-actions>
        <button  mat-raised-button type="button" (click)="onNoClick()" color="primary">Close</button>
        <button  mat-raised-button type="submit" 
            [disabled]="!(loginForm.controls.email.valid && loginForm.controls.password.valid)" 
            color="accent">
            Login
        </button>
    </mat-card-actions>
Run Code Online (Sandbox Code Playgroud)