question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Passing data to modal with a reactive form

See original GitHub issue

I have a form that I want to use for both posting and editing data. I’m creating the modal using the service and passing the object that needs to be edited in the content.

I’m creating the from in the constructor, but at that time the data from the main component is not available in the modal component, nor is it available in the OnInit call.

Any ideas on how to make this work?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
dushkostanoeskicommented, Aug 24, 2017

OK, so misread the the docs, forget my last comment 😄

This is my parent component function call that creates the modal:

editNalog(id: number) {
    this.nalogModal = this.modalService.show(NalogModalComponent);
    this.nalogModal.content.nalog = this.model.nalozi.find(x => x.id === id);
    this.nalogModal.content.modalShown = true;
}

The constructor in the Modal component:

constructor(public bsModal: BsModalRef) {
    console.log(this.nalog);
}

And the OnInit function:

ngOnInit(): void {
   console.log(this.nalog);
   setTimeout(() => {
       console.log(this.nalog);
   },
   0);
}

In the console I have

undefined
undefined
{ id: 952 ...}

Got it to work thanks to your hack, but I wouldn’t close this issue yet because this should work as per the documentation.

2reactions
krooshuacommented, Aug 24, 2017

Hi, @dushkostanoeski I think, in your case, data not yet actually bound to bsModalRef. Try to wrap data binding with time out. It looks like a hack, but should work.

ngOnInit() {
	//Bind data from bsModalRef to component
	setTimeout(() => {
		if (this.bsModalRef.content && this.bsModalRef.content.someData) {
			this.someData = this.bsModalRef.content.someData;
		}
	}, 0);	
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Passing data to modal and binding it to a reactive form
I have a modal that has a reactive form with a few inputs and I want to use the same modal for both...
Read more >
Reactive forms in angular 13 (CRUD + bootstrap modal + ...
reactiveforms #angular13 #angularformsThis video explains reactive forms in angular 13 also includes below topics1, Reactive Form ...
Read more >
Pass Data into Ng-Bootstrap Modal in Angular 8
Pass data in Ng-Bootstrap Modal. In this tutorial, we'll learn how to pass data to a modal by using Ng-Bootstrap in Angular version...
Read more >
Reactive Forms with Custom Validations in ngBootstrap Modal
In this post we will cover implementation of bootstrap modal along with angular's reactive forms , custom validations and child-parent ...
Read more >
Angular Material 9|8|7 Add Dialog Modal and Pass Data ...
After adding Angular material in our project, we need to import the required modules in the Angular module. we'll use Dialog, Input Field,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found