Angular 9-订阅中的变量更改后,视图将不会更新

时间:2020-07-04 18:13:50

标签: angular angular9 subscribe

在Anglar 9中使用对HTTP客户端进行订阅时,我无法使其在HTML部分中具有持久性。我尝试了以下stackoverflow帖子-

  1. documentation
  2. implementation

以下是我正在尝试的代码

library(dplyr)

df <- structure(list(Operator = c("United", "American", "United", "United", 
"American", "American", "Delta", "United"), Satellite = c("SAT1", 
"SAT2", "SAT1", "SAT3", "SAT3", "SAT5", "SAT1", "SAT8")), class = "data.frame", row.names = c(NA, 
-8L))

#Code
df %>% left_join(df %>% group_by(Satellite) %>% summarise(N=n())) %>% filter(N>1)

  Operator Satellite N
1   United      SAT1 3
2   United      SAT1 3
3   United      SAT3 2
4 American      SAT3 2
5    Delta      SAT1 3

2 个答案:

答案 0 :(得分:2)

如果您使用onInit策略,则您的onPush函数应该是这样的:

 ngOnInit(): void {
     return this.http
            .get(environment.apiBaseUrl + url, this.getOptions())
            .pipe(takeUntil(this.unsubscribe$))
            .pipe(retry(3), catchError(this.handleError));
            .subscribe(response => {
                this.list = response.data;
                this.changeRef.markForCheck();
            });

  }

答案 1 :(得分:1)

我认为您应该使用async管道使您的生活更轻松。

ngOnInit(): void {
    let urURl = 'https://reqres.in/api/products';
     this. list$ = this.http
            .get(urURl)

  }

app.component.html

{{list $ | async | json}}

我在stackblitz上为u创建了一个演示