Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass data to Angular ng-bootstrap modal for binding

Scenario:

  • ChildComponent - having a lot ngModel binding elements.
<input [(ngModel)]="TryToPassDataModel.name">;
  • ParentComponent -
btn.onClick = function() {
  this.bsModalRef =
    this.modalService.open(ChildComponent, TryToPassDataModel);
}

This works in ngx-bootstrap, but how to implement this in nb-bootstrap? (it looks like such a simple idea)

like image 790
Jiping Avatar asked Feb 09 '18 04:02

Jiping


People also ask

How do I pass data to modal popup?

To pass data into the modal body jquery methods are used. jQuery is similar to JavaScript, however jQuery methods are simple and easier to implement. jQuery reduces the lines of code. The article demonstrates two examples where data is passed into the modal body from the HTML document body.

What is NgbActiveModal?

If you pass a component type as content, then instances of those components can be injected with an instance of the NgbActiveModal class. You can then use NgbActiveModal methods to close / dismiss modals from "inside" of your component. Also see the NgbModalOptions for the list of supported options. dismissAll.


2 Answers

Looks like you're not using the API properly. The plugin expects the params to be passed as @Input(). Something like this would work :

const modalRef = this.modalService.open(NgbdModalContent);
modalRef.componentInstance.name = 'World';

Make sure you add a @Input for your model in ModalContent component!

See the doc for more info : https://ng-bootstrap.github.io/#/components/modal/examples

like image 95
user3220633 Avatar answered Sep 27 '22 19:09

user3220633


In Angular 8 using ng-bootstrap Modal to pass data from parent component to modal

Source Link

enter image description here

openModal() {
const modalRef = this.modalService.open(MyBootstrapModalComponent,
  {
    scrollable: true,
    windowClass: 'myCustomModalClass',
    // keyboard: false,
    // backdrop: 'static'
  });

let data = {
  prop1: 'Some Data',
  prop2: 'From Parent Component',
  prop3: 'This Can be anything'
}

modalRef.componentInstance.fromParent = data;
modalRef.result.then((result) => {
  console.log(result);
}, (reason) => {
});
}
like image 20
Code Spy Avatar answered Sep 27 '22 17:09

Code Spy