That's the way I use the ng2-bootstrap modal:
import {Component} from '@angular/core';
import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'add-customer-modal',
template: `
<template #test let-c="close" let-d="dismiss">
<div class="modal-header">
<button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" (click)="c('Close click')">Close</button>
</div>
</template>
<button type="button" class="btn btn-primary btn-sm" (click)="open(test)"><i class="fa fa-plus"></i> <i class="fa fa-user-o"></i></button>
`
})
export class AddCustomerModal {
constructor(private modalService: NgbModal) {}
open(content) {
this.modalService.open(content, { size: 'lg' }).result.then((result) => {
console.log(result);
}, (reason) => {
console.log(reason);
});
}
}
I'am a little bit confused, because I thought the content is used to pass parameters to the modal. But in my opinion it's only the name the open method needs to find the correct template?
So how can I pass parameters?
When the submit button is clicked, it invokes the jQuery function. The data entered the text area is extracted using the val() method into the text variable. This text string is passed to the modal body using the html() method of jQuery.
Create the input fields inside the modal window with the id that are passed in find method. That will put the values passed in the input fields inside modal window.. Show activity on this post.
Load Dynamic Content from Database in Bootstrap ModalBy clicking the Open Modal ( . openBtn ) button, the dynamic content is loaded from another PHP file ( getContent. php ) based on the ID and shows on the modal popup ( #myModal ).
To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.
Anyone still stumbling onto this might find this handy, all you need to do is declare a field inside your ModalComponent like this:
modalHeader: string;
advertiser: Advertiser;
You can set these fields by doing the following when you are opening a modal.
advertiserModalShow() {
const activeModal = this.modalService.open(AdvertiserModal, { size: 'lg' });
activeModal.componentInstance.modalHeader = 'Advertiser';
activeModal.componentInstance.advertiser = this.selectedAdvertiser;
}
In your template you can access them like this:
value={{advertiser.code}}
Hope this helps.
To pass parameters/data to the modal, my guess would be to use the componentInstance
:
open(content) {
const modal: NgbModalRef = this.modalService.open(content, { size: 'lg' });
(<MyComponent>model.componentInstance).data = 'hi';
modal.result.then((result) => {
console.log(result);
}, (reason) => {
console.log(reason);
});
}
This assumes that the componentInstance is of type MyComponent
and that it has a public property data
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