1

Angular 8 のサービスの関数に http パッチ呼び出しがあります。

// public returnStr: string = "";  at top of service

updateResponse(response: Response, reviewId: number): string {
    this.http.patch(`/assessing/api/reviews/update/${reviewId}`, response, this.httpOptions)
        .subscribe(() => {
            console.log("Success updating response");
            this.returnStr = "Success updating response";
        },
            response => {
                console.log("Error in response patch call: ", response);
                this.returnStr = "Failed to update";
            },
            () => {
                console.log("Patch call for response complete");
            });
    return this.returnStr;
}

このサービス関数は、コンポーネントによって呼び出されます。

save(response: Response): string {
        this.returnStr = this.dataservice.updateResponse(response, this.reviewId);
        console.log("this.returnStr = " + this.returnStr);
    }

this.returnStr は、html の上部に出力されます。

「保存」ボタンが初めてクリックされたときに何が起こるかというと、コンソールでは次のようになります。

this.returnStr = 
Success updating response
Patch call for response complete

その後、コンソールで:

this.returnStr = Success updating response
Success updating response
Patch call for response complete

したがって、http.patch は正常に動作します。私の唯一の懸念は、html ページに表示される this.returnStr の値です。最初の関数呼び出しでも、 this.returnStr が空の文字列ではなく「成功の更新応答」になるようにするにはどうすればよいですか? または、最初の呼び出し後も空の文字列のままなのはなぜですか? エラーの場合も同じパターンが発生します -- this.returnStr は、最初の関数呼び出しで「更新に失敗」し、エラーが返されるはずですが、そうではありません。

ありがとう

4

2 に答える 2