如何在 $http.get returns 之后调用函数

how to call a function on after $http.get returns

我已经实现了 ngrx-store。我试图在 http 调用之前打开微调器。并在电话返回后将其关闭。

   getInspectionDetails(order) {
    this.store.dispatch({ type: SPINNER_VISIBLE, payload: true }) //<-- spinner on
    return this.$http.get(this.url+'api/Inspection/'+order.documentNumber)
        .map(this.httpHelper.extractData)
        .catch(this.httpHelper.handleError)
        .map(payload => ({ type: INSPECT_PURCHASE_ORDER, payload }))
        .subscribe(action => this.store.dispatch(action))});
}

现在我正在尝试使用

this.store.dispatch({ type: SPINNER_VISIBLE, payload: false }) 

关闭微调器。基本上它与使用错误有效负载关闭微调器的调用相同。 但是我应该把它放在哪里?

subscribe 方法有 3 个参数:

httpRequest.subscribe(
    action => // do something when the value arrives
    error => // do something when error occurres
    () => // do something when observable completes
);

或者您可以在可观察对象上使用 finally 方法:

getInspectionDetails(order) {
  this.store.dispatch({ type: SPINNER_VISIBLE, payload: true }) //<-- spinner on
  return this.$http.get(this.url+'api/Inspection/'+order.documentNumber)
      .map(this.httpHelper.extractData)
      .catch(this.httpHelper.handleError)
      .finally(() => this.store.dispatch({ type: SPINNER_VISIBLE, payload: false }))
      .map(payload => ({ type: INSPECT_PURCHASE_ORDER, payload }))
      .subscribe(action => this.store.dispatch(action));
}

第二种方法可能更好,因为在发生错误时不会触发完整的回调。更多关于这个问题的区别:

https://github.com/angular/angular/issues/7865