14

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)

0

2 Answers 2

21

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

Sign up to request clarification or add additional context in comments.

3 Comments

This approach has a major problem: lifecycle hooks like ngOnInit are called before the variables are binded to the component.
To follow the changes on Input parametters, you should be using the lifecycle hook OnChanges : angular.io/api/core/OnChanges. I haven't worked with angular in a while, but I think NgAfterViewInit will also work as you expect. Basicly ngOnInit is called when the component is constructed so the params don't have time to be propagated.
Sure :) In addition though, this pattern introduces another problem of change detection. I filed an issue at ng-bootstrap's GitHub.
4

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) => {
});
}

1 Comment

This solution doesn't work if you require the data to be available in the component when it is created. Throw console.log(this.fromParent) in ngOnInit() {} in the modal and it will appear to work. Simulate a delay with setTimeout(() => {modalRef.componentInstance.fromParent = data}, 5000); on the parent and now you'll get undefined when opening the component.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.