Как загрузить сообщение об ошибке в шаблон после запуска отправки, когда в бэкенде произошла ошибка - PullRequest
0 голосов
/ 26 февраля 2020

У меня есть приложение anuglar 8. И я хочу показать сообщение после срабатывания кнопки подтверждения.

У меня есть этот метод:


  @ViewChild(IsLoadingComponent, {static: false}) isLoading: IsLoadingComponent;

submit() {
    if (this.sending) {
      return;
    }

    // submit the answers and initialvalues, it is possible that the currentpage has not changed but we save it anyway.
    if (this.isSubmittable()) {
      this.sending = true;

      const answers = this.echeqService.getAnswers();

      this.healthAPI
        .postSingleEcheqSubmission(this.currentEcheqSubmission.id, {
          answers: answers,
          initialValues: {},
          currentPage: this.currentEcheqSubmission.currentPage,
          progress: this.currentEcheqSubmission.progress
        })
        .subscribe(
          results => {
            this.handleSubmitSuccess();
          },
          error => {
            setTimeout(() => {
              this.handleSubmitError('Vcheq kan niet worden verstuurd');

            }, 1000);
          }
        );
    }
  }

и handleSubmitError выглядит так:


  handleSubmitError(msg: string) {
    console.log(this.isLoading);
    this.isLoading.displayLoadErrorMessage(msg);
  }

и шаблон кнопки отправки выглядит следующим образом:

  <div class="echeq-send-button" (click)="submit()" [ngClass]="{ disabled: sending }">
          send <span class="fa fa-refresh fa-spin echeqprogress-spinner" *ngIf="sending"></span>
        </div>

Но

isLoading инициализируется при загрузке компонента.

Но когда я нажимаю на кнопку отправки button: this.isloading не определено:

 handleSubmitError(msg: string) {
    console.log(this.isLoading);
    this.isLoading.displayLoadErrorMessage(msg);
  }

и я получаю эту ошибку, когда нажимаю submit:

core.js:7376 ERROR TypeError: Cannot read property 'displayLoadErrorMessage' of undefined
    at EcheqProcessComponent.push../src/app/echeq/echeq-process/echeq-process.component.ts.EcheqProcessComponent.handleSubmitError (echeq-process.component.ts:292)
    at echeq-process.component.ts:252

Так что мне нужно изменить? Спасибо

Я пытаюсь это так:

  <div class="echeq-send-button" (click)="submit()" [ngClass]="{ disabled: sending }" (error)="handleSubmitError($event)">

          send <span class="fa fa-refresh fa-spin echeqprogress-spinner" *ngIf="sending"></span>
        </div>

1 Ответ

0 голосов
/ 26 февраля 2020

Я решил, вот так:

 submit() {
    if (this.sending) {
      return;
    }

    // submit the answers and initialvalues, it is possible that the currentpage has not changed but we save it anyway.
    if (this.isSubmittable()) {
      this.eCheqLoaded = false;
      this.eCheqError = false;

      this.sending = true;

      const answers = this.echeqService.getAnswers();

      this.healthAPI
        .postSingleEcheqSubmission(this.currentEcheqSubmission.id, {
          answers: answers,
          initialValues: {},
          currentPage: this.currentEcheqSubmission.currentPage,
          progress: this.currentEcheqSubmission.progress
        })
        .subscribe(
          results => {
            this.handleSubmitSuccess();
          },
          error => {
            this.eCheqLoaded = false;
            this.eCheqError = false;

            setTimeout(() => {
              this.handleSubmitError('vcheq kan niet worden verstuurd')
              //this.isLoading.displayLoadErrorMessage('Vcheq kan niet worden verstuurd');
            }, 500);
          }
        );
    }
  }
...