【问题标题】:How to show http get from angular 4 response on html如何在html上显示来自角度4响应的http获取
【发布时间】:2018-04-11 07:07:57
【问题描述】:

我正在尝试使用 Angular 4 中的 http get 方法从表中获取数据。如何在 html 页面上显示它?

getData(){
  //return this.data;
  return this.http.get("http://localhost:8080/SpringRestExample/get/filedata")
                  .map((response: Response) => 
                      {
                           return response.json();
                   });
}

组件.ts

getPosts(): void {
console.log("In"+this.service.getData());
this.service.getData()
    .subscribe(
        resultArray => this.data = resultArray,
        error => console.log("Error :: " + error)
    )
    console.log(this.data);
}

这里我的数据显示未定义,但 get 方法显示返回了对象。我无法获取它并在 html 上显示它?

【问题讨论】:

标签: html angular web get


【解决方案1】:

将您的控制台日志方法放在订阅方法中。

【讨论】:

    【解决方案2】:

    您在更新前访问this.data。这样做:

    getPosts(): void {
        console.log("In"+this.service.getData());
        this.service.getData()
            .subscribe(resultArray => {
                    this.data = resultArray;
                    console.log(this.data);
                    // update data source
                    this.dataSource.data = this.data;
                },
                error => console.log("Error :: " + error)
            );
    }
    

    【讨论】:

    • 嗨 Ashuman,我可以看到数据的价值,但屏幕没有显示任何作为异步调用的内容。知道如何处理吗?
    • 你能发布 HTML 吗?
    • 没有可能是由于 aynchrounus 调用
    • 我想检查一下您是如何在 HTML 中访问它的。
    • 文件名 ​​eader-cell> {{element.filename}}
    猜你喜欢
    • 2019-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 2018-05-31
    • 2019-01-18
    • 1970-01-01
    相关资源
    最近更新 更多