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.

Modals (or others) with component as content does not trigger ngOnChanges

See original GitHub issue

This is really nice:

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

Anyway this does not trigger ngOnChanges() when we set the input “name” but this lifecycle-hook are a crucial feature of Angular components.

Please consider this as a feature request. Also i am looking for a workaround to trigger ngOnChanges manually.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:4
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
ip-amity-taruncommented, Dec 12, 2019

private cd_interval_fn: any; constructor(private cdref: ChangeDetectorRef ) { this.cdref.detach(); const This = this; this.cd_interval_fn = setInterval(() => { This.cdref.detectChanges(); }, 500); } ngOnDestroy() { clearInterval(this.cd_interval_fn); this.cdref.detach(); }

This works for me.

1reaction
dstjcommented, Dec 11, 2019

@IAfanasov , your proposed workaround didn’t work for me. ngOnChanges still wouldn’t get triggered. 😕

The only workaround I got working was to wrap the component with ngOnChanges in another component that only forwards the binding.

Like this:

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

...

@Component({
	selector: 'app-modal-content-modal',
	template: `<app-inner-modal-content [name]="name"></app-inner-modal-content>`,
	changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ModalContentComponent {
	@Input() name: string;
}

@Component({
	selector: 'app-inner-modal-content-modal',
	template: `...`,
	changeDetection: ChangeDetectionStrategy.OnPush,
})
export class InnerModalContentComponent implements OnChanges {
	@Input() name: string;

	ngOnChanges(changes: SimpleChanges) {
		console.log('ngOnChanges triggered');
	}
	...
}

Using Angular v7.2.15 (if ever that changes anything)

Read more comments on GitHub >

github_iconTop Results From Across the Web

ngOnChanges not trigger when setting property directly
When you set the component instance's values directly, it will not trigger an ngOnChanges. ngOnChanges is triggered when the value binding ...
Read more >
Testing Components – Testing Angular
Introduction to testing Angular Components with Angular's TestBed. ... triggerEventHandler does not simulate event bubbling or any other ...
Read more >
Angular / Angular guidelines and best practices / Testing
Writing good quality tests for critical functionalities is an important step in quality assurance. We do not recommend not writing any tests ...
Read more >
Heading Off 11 Common Angular Application Design, Best ...
Often this is seen with modal windows, application level alerts, or other event ... This is not to say the dumb components have...
Read more >
Angular View Updating Using Observables, Behavior Subject ...
What if the form is in a child component, say in a modal? Oh shoot. No worries! This is when event emitters come...
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