Reputation: 1588
I've the following table with a modal popup using bootstrap 4 in angular
<tbody *ngFor="let data of json | sizeFilter : sizeInput">
<tr>
<td (click)="onUpdateClick(data.hash , data.size, data.time)" class="hHover"
title="Click to update the data">{{data.hash}}</td>
<td>{{data.size}}</td>
<td>{{data.time}}</td>
<td>
<td>
<!-- Button trigger modal -->
<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
Delete
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog"
aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Are you sure?</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Are you sure you that you want to delete the block?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary"
data-dismiss="modal">Close</button>
<button type="button" (click)="onDeleteClick(data.hash)" class="btn btn-primary">Delete</button>
</div>
</div>
</div>
</div>
</td>
</tr>
</tbody>
THe problem is that the popup isn't being displayed on popup. Everything seems fine for me so im stuck.
Upvotes: 0
Views: 3700
Reputation: 1249
In Angular, some components of Bootstrap, like the modals, work better with dedicated library, such as ng-bootstrap. This library let you open and close the modal and send variables at opening and closing. Use it this way (I took the code of the first example of this page of the documentation) :
In your .html file :
<button class="btn btn-lg btn-outline-primary" (click)="open(content)">Launch demo modal</button>
<ng-template #content let-modal>
<div class="modal-header">
<h4 class="modal-title" id="modal-basic-title">Profile update</h4>
<button type="button" class="close" aria-label="Close" (click)="modal.dismiss('Cross click')">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="dateOfBirth">Date of birth</label>
<div class="input-group">
<input id="dateOfBirth" class="form-control" placeholder="yyyy-mm-dd" name="dp" ngbDatepicker #dp="ngbDatepicker">
<div class="input-group-append">
<button class="btn btn-outline-secondary calendar" (click)="dp.toggle()" type="button"></button>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-dark" (click)="modal.close('Save click')">Save</button>
</div>
</ng-template>
In your .ts file :
import {Component} from '@angular/core';
import {NgbModal, ModalDismissReasons} from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'ngbd-modal-basic',
templateUrl: './modal-basic.html'
})
export class NgbdModalBasic {
closeResult: string;
constructor(private modalService: NgbModal) {}
open(content) {
this.modalService.open(content, {ariaLabelledBy: 'modal-basic-title'}).result.then((result) => {
this.closeResult = `Closed with: ${result}`;
}, (reason) => {
this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
});
}
private getDismissReason(reason: any): string {
if (reason === ModalDismissReasons.ESC) {
return 'by pressing ESC';
} else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
return 'by clicking on a backdrop';
} else {
return `with: ${reason}`;
}
}
}
Also, be careful in your code, as your modal div is in the loop, the id is repeated multiple time and is not unique. The code of the modal must be outside of the loop.
Upvotes: 2