角度订阅问题

时间:2018-09-24 18:47:18

标签: angularjs angular rxjs

我需要最终计数始终为1,这是存储失败请求计数。我也不确定为什么在“初始计数”之前打印“最终计数”。我可能无法正确理解订阅。希望这是有道理的。

 public failed: number = 0;

    buttonClickMethod(): void {
        // request code

        Observable.onErrorResumeNext(myrequests).first().subscribe(
            () => {
                // On next code
            },
            () => {
                this.failed++;
                console.log('Initial count' + this.failed)
            },
            () => {
                //On completed code
            }
        });
    console.log('Final count' + this.failed)
    this.failed = 0;

    }

1 个答案:

答案 0 :(得分:1)

public failed: number = 0;

buttonClickMethod(): void {
    // request code
    Observable.onErrorResumeNext(myrequests).first().subscribe(
        () => {
            // On next code
        },
        () => {
            this.failed++;
            console.log('Initial count' + this.failed)
        },
        () => {
            //On completed code
        }
    });
console.log('Final count' + this.failed)
this.failed = 0;

}

您的console.log语句是单击按钮后立即执行的,与您的Observable本质上是async不同。

看看异步调用是如何工作的。