Home > Blockchain >  call ngOnit if there are changes
call ngOnit if there are changes

Time:10-13

First is that I have a parent component that will show the dialog in initialization. In the dialog the user will provide input and then validated .

I want to call ngOnit again to open the dialog if the response from this.dialogRef.close(res) value is false.

Can we detect that on ngOnchanges? Thanks.

#dialog-component snippet

 validateInput() {
    console
    this.service.validate(key).subscribe(
      (res) => {
        this.dialogRef.close(res);

      },
      (err) => {
        AppUtils.errorHandler(err, this._notificationService, this.dialog);
      }
    );
  }

#parent component

 ngOnInit(): void {
    this._router.paramMap.subscribe((paramMap) => {
      if (paramMap.get('authkey')) {
        const dialogRef =  this.dialog.open(DialogComponent, {
          panelClass: 'document-management-rename-dialog',
          width: '400px',
          data: {
            key: paramMap.get('authkey').replace('authkey:', '')
          }
        });
        dialogRef.afterClosed().subscribe((result) => {
          if (result.isSuccess) {
            this.showPage = true;
          }
        });
      }
    });
  }

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

CodePudding user response:

to re-open the dialog you can use a Subject and each time this Subject emits a value open dialog. Remember to unsubscribe from observables a working example is here: https://stackblitz.com/edit/angular-ivy-4zitic?file=src/app/app.component.ts

private handleDialogResult = new Subject();

public ngOnInit(): void {
  this.handleDialogResultSub = this.handleDialogResult
    .asObservable()
    .subscribe(() => {
      this.openDialog();
    });

  this._router.paramMap.subscribe((paramMap) => {
    if (paramMap.get('authkey')) {
      this.openDialog();
    }
  });
}

public openDialog(): void {
  const dialogRef =  this.dialog.open(DialogComponent, {
    panelClass: 'document-management-rename-dialog',
    width: '400px',
    data: {
      key: paramMap.get('authkey').replace('authkey:', '')
    }
  });
  dialogRef.afterClosed().subscribe((result) => {
    if (result.isSuccess) {
      this.showPage = true;
      return;
    }
    this.handleDialogResult.next(true);
  });
}
  • Related