I want to prevent from the user to click outside the modal dialog and he only can press on the button to exit from the dialog. how can I do that?
dialog.component.ts
ngOnInit() {
const dialogRef = this.dialog.open(DialogResultComponent);
dialogRef.afterClosed().subscribe(result => {
console.log(result);
});
}
dialog-result.component.ts
import { Component, OnInit } from '@angular/core';
import { MdDialog, MdDialogRef } from '@angular/material';
import { FormGroup,FormControl,Validators,FormBuilder, } from '@angular/forms';
@Component({
selector: 'app-dialog-result',
templateUrl: './dialog-result.component.html',
})
export class DialogResultComponent {
form: FormGroup;
name = new FormControl('',Validators.required);
width = new FormControl('',Validators.required);
height = new FormControl('',Validators.required);
constructor(public dialogRef: MdDialogRef<DialogResultComponent>,private fb: FormBuilder) {
}
ngOnInit() {
this.form = this.fb.group({
'name' :this.name,
'width': this.width,
'height': this.height,
});
}
saveData(){
console.log(this.name.value);
this.dialogRef.close({name:this.name.value,width:this.width.value,height:this.height.value});
}
}
what I tried to do is: dialog-result.component.html
<div>
<form [formGroup]="form">
<h3>MineSweeperwix</h3>
<div class="mdl-dialog__content">
<p><mdl-textfield type="text" label="name" ([ngModel])="name" floating-label autofocus></mdl-textfield></p>
<mdl-textfield type="number" formControlName="width" label="width" floating-label autofocus></mdl-textfield>
<mdl-textfield type="number" formControlName="height" label="height" floating-label autofocus error-msg="'Please provide a correct email!'"></mdl-textfield>
</div>
<div class="mdl-dialog__actions">
<button mdl-button (click)="saveData()" mdl-button-type="raised" mdl-colored="primary" mdl-ripple [disabled]="!form.valid">Save</button>
<button mdl-button (click)="dialogRef.close(dd)" mdl-button-type="raised" mdl-ripple>Cancel</button>
</div>
</form>
</div>
dialog-result.component.cs
div.modal-backdrop {
position: fixed;
left: 0;
right: 0;
top: 0;
bottom: 0;
z-index: 100; /* less than your dialog but greater than the rest of your app */
/* optional: */
background: black;
opacity: 0.2;
}
When the Button is clicked, the HTML DIV is referenced using jQuery and its modal function is called along with properties data-backdrop: "static" and data-keyboard: false which disables the closing of the Bootstrap Modal Popup when clicked outside.
You can prevent closing of modal dialog by setting the beforeClose event argument cancel value to true. In the following sample, the dialog is closed when you enter the username value with minimum 4 characters. Otherwise, it will not be closed.
Adding a demo too as Mike said,
openDialog() {
let dialogRef = this.dialog.open(DialogResultExampleDialog,{disableClose:true});
dialogRef.afterClosed().subscribe(result => {
this.selectedOption = result;
});
}
LIVE DEMO
You're using the Material Design dialog, which has an option to add a backdrop and prevent closing.
I think you need to do something like this:
this.dialogRef = this.dialog.open(DialogResultComponent, {
disableClose: true,
hasBackdrop: true // or false, depending on what you want
});
See the demo at https://github.com/angular/material2/blob/master/src/demo-app/dialog/dialog-demo.ts.
Because the documentation is not yet ready, I've found it invaluable to look at their demo app, which is included in the source. You can run it locally with:
npm run demo-app
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With