0

I have a parent component with the following function:

@ViewChild('currentTab') currentTab: ChildComponent;  
  
nextTab(): void {
  if (this.currentTab.save()) {
    this.activeIndex++; 
  }
}

And the following method on the child component:

save(): boolean {
  return this.confirmationService.confirm({
    message: 'Are you sure?',
    accept: () => true,
    reject: () => false
  });
}

The problem: The conditional in the parent component doesn't wait for an answer, it gets as 'true', doing the this.activeIndex++; . What am I doing wrong?

Thanks in advance.

Ethan Vu
  • 2,911
  • 9
  • 25

1 Answers1

0

Javascript is always synchronous and single-threaded. So the expression this.currentTab.save() is resolved and if(....) statement is evaluated immediately.As per PrimeNg source code the confirm(...) method fire a signal to display the popup dialog then return this which is a truthy value. So It is expected that activeIndex always increase.

To achive what you want, import and use the confirm service in the parent component :

nextTab(): void {
  this.confirmationService.confirm({
     message: 'Are you sure?',
     accept: () => this.activeIndex++,
     reject: () => //Do something else
  });
}
Ethan Vu
  • 2,911
  • 9
  • 25