کار با modal dialogs مجموعه Bootstrap در برنامههای Angular
نویسنده: وحید نصیری
تاریخ: ۱۳۹۶/۰۹/۱۲ ۱۸:۳۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
import { BsDropdownModule } from "ngx-bootstrap/dropdown";
import { TooltipModule } from "ngx-bootstrap/tooltip";
import { ModalModule } from "ngx-bootstrap/modal";
@NgModule({
imports: [
CommonModule,
BsDropdownModule.forRoot(),
TooltipModule.forRoot(),
ModalModule.forRoot()
],
exports: [
BsDropdownModule,
TooltipModule,
ModalModule
]
})
export class SharedBootstrapModule { } import { SharedBootstrapModule } from './shared/shared.bootstrap.module';
@NgModule({
imports: [BrowserModule, SharedBootstrapModule],
// ...
})
export class AppModule {} import { SharedBootstrapModule } from "./shared.bootstrap.module";
@NgModule({
imports: [
CommonModule,
SharedBootstrapModule
],
exports: [
CommonModule,
SharedBootstrapModule
]
}) import { Component, OnInit, TemplateRef } from "@angular/core";
import { BsModalRef, BsModalService } from "ngx-bootstrap";
@Component({
selector: "app-modal-dialog-test",
templateUrl: "./modal-dialog-test.component.html",
styleUrls: ["./modal-dialog-test.component.css"]
})
export class ModalDialogTestComponent implements OnInit {
modalRef: BsModalRef;
constructor(private modalService: BsModalService) { }
openModal(template: TemplateRef<any>) {
this.modalRef = this.modalService.show(template,
{ animated: true, keyboard: true, backdrop: true, ignoreBackdropClick: false });
}
closeModal() {
this.modalRef.hide();
}
} <h1>Displaying modal bootstrap dialogs</h1>
<button type="button" class="btn btn-info" (click)="openModal(template1)">Create template modal</button>
<ng-template #template1>
<div class="modal-header">
<h4 class="modal-title pull-left">Modal</h4>
<button type="button" class="close pull-right" aria-label="Close" (click)="closeModal()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
This is a modal.
</div>
</ng-template>
ng g c Shared/ConfirmModal --skip-import
import { ConfirmModalComponent } from "./confirm-modal/confirm-modal.component";
@NgModule({
imports: [
],
entryComponents: [
ConfirmModalComponent
],
declarations: [
ConfirmModalComponent
]
})
export class SharedModule {} import { Component } from "@angular/core";
@Component({
selector: "app-confirm-modal",
templateUrl: "./confirm-modal.component.html",
styleUrls: ["./confirm-modal.component.css"]
})
export class ConfirmModalComponent {
args: {
title: string;
message: string;
};
close: (val?: any) => void;
} <div class="modal-header">
<h4 class="modal-title pull-left">{{ args?.title }}</h4>
<button type="button" class="close pull-right" aria-label="Close" (click)="close()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>{{ args?.message }}</p>
</div>
<div class="modal-footer">
<button class="btn btn-danger" (click)="close(true)">Yes</button>
<button class="btn btn-primary" (click)="close()">Cancel</button>
</div> >ng g s Core/Modal
import { Injectable } from "@angular/core";
import { BsModalService } from "ngx-bootstrap";
@Injectable()
export class ModalService {
constructor(private bsModalService: BsModalService) { }
show(component: any, args?: any, options?: any): Promise<any> {
return new Promise(resolve => {
options = options || {};
const modal = this.bsModalService.show(component, options);
let result: any;
const sub = this.bsModalService.onHidden.subscribe(() => {
sub.unsubscribe();
resolve(result);
});
modal.content.args = args;
modal.content.close = (val?: any) => {
result = val;
modal.hide();
};
});
}
} import { ModalService } from "./modal.service";
@NgModule({
providers: [
ModalService
]
})
export class CoreModule {} <button type="button" class="btn btn-danger" (click)="deleteRecord()">Delete record</button>
<div *ngIf="confirmResult" class="alert alert-info">{{confirmResult}}</div> import { ModalService } from "./../../core/modal.service";
import { ConfirmModalComponent } from "./../../shared/confirm-modal/confirm-modal.component";
export class ModalDialogTestComponent implements OnInit {
confirmResult: string;
constructor(private modalService: ModalService) { }
deleteRecord() {
this.confirmResult = "";
this.modalService.show(
ConfirmModalComponent,
{
title: "Confirm", message: "Do you want to delete this record?"
},
{
animated: true, keyboard: true, backdrop: true, ignoreBackdropClick: false
}).then(confirmed => {
if (confirmed) {
this.confirmResult = "Deleted!";
} else {
this.confirmResult = "Canceled!";
}
});
}
}
ERROR TypeError: "this.fileInput2 is undefined"
<ng-template #templateUploads>
<div>
<strong>{{titleModal}}</strong>
<button type="button" aria-label="Close" (click)="closeModal()">
<span aria-hidden="true">×</span>
</button>
</div>
<div>
<form #upladForm="ngForm" (submit)="submitUpload()" novalidate>
<fieldset>
<div>
<label>فایل</label>
<div>
<input required #fileInput2 type="file">
</div>
</div>
<div>
<button (click)="upload()" type="button">
upload
</button>
<button type="submit" >
close
</button>
</div>
</fieldset>
</form>
</div>
</ng-template> @ViewChild('fileInput2') fileInput2: ElementRef;
constructor(......
submitUpload(form: NgForm) {
const fileInput2File: HTMLInputElement = this.fileInput2.nativeElement;
.....