【问题标题】:Angular2 cannot display the data from http when using route.paramsAngular2 使用 route.params 时无法显示来自 http 的数据
【发布时间】:2017-03-24 15:20:49
【问题描述】:

我正在学习 Angular2 从 $http GET 请求中获取数据。目前,数据不显示。我认为这是因为我的服务在我的组件渲染后传递了数据。我做了一些搜索,但仍然没有得到它。我在代码中添加了两个console.log(data),这是我的控制台截图。

这是我在 arr-detail.component.ts 中的ngOnInit()

import { TbArr } from './../../shared/models/tb_arr';
private data: TbArr;
ngOnInit() {
//method 1
  this.route.params
  .switchMap((params: Params) => this._dbService.get('MyAPI.php', 'id=' + params['id']))
  .subscribe((d:TbArr) => this.data = d)
  console.log(this.data);

/**method 2
* this.id = this.route.snapshot.params['id'];
*  this._dbService.get('MyAPI.php', 'id=' + this.id)
*       .subscribe(d => {
*         this.data = d;
*       });
**/
/**method 3
* this.route.params.subscribe(params => {
*   if (params['id']) { //got the id from url
*     this._dbService.get('MyAPI.php', 'id=' + params['id'])
*       .subscribe(d => {
*         this.data = d;
*       });
*   }
* });
**/
}

这是我在 db.service.ts 中的 GET 函数:

get(api: string, options: any) {
    return this.dbRequest('get', api, options);
}
dbRequest(method: string, api: string, options: string) {

    let observable: any;
    const backendUrl_json = 'http://XXX.XX.XX.XX/api/json/';
    observable = this.http[method](backendUrl_json + api, {search: options});
    observable = observable.map(res => res.json());
    console.log(method, backendUrl_update + api, options);
    observable.subscribe(
                d => { console.log(d); },
                e => { console.log(e); }
            );
    return observable;
}

这是我的组件 html 文件:

<table *ngIf="data">
  <tbody>
    <tr>
      <td>ID:</td>
      <td>{{data?.id | async}}</td>
    </tr>
    <tr>
      <td>Date:</td>
      <td>{{data?.date | async}}</td>
    </tr>
   </tbody>
 </table>

这是我的 tb_arr,它有 TbArr 接口:

export interface TbArr {
   id:number;
   date:any;
}

更新我的问题: 表格显示在网页上,但没有显示数据。
当我在控制台中打开“对象”时,我得到了这个:

有人对此有任何想法吗?非常感谢你。如果您需要更多信息,请给我留言。

【问题讨论】:

  • 您能否添加您的 json 的样子...或者更准确地说,当您 console.log 时数据的样子?
  • @AJT_82 我添加了控制台截图。

标签: angular angular2-routing angular2-services


【解决方案1】:

您可以从您的 console.log 中看到您正在处理一个数组中的对象。如果您总是只接收数组中的一个对象,请从数组中提取它.... 订阅时从数组中提取对象,或者您需要在模板中适当地访问它,这意味着:

{{data[0].id}}

这里我选择提取订阅中的对象:

 this.route.params
  .switchMap((params: Params) => this._dbService.get('MyAPI.php', 'id=' + params['id']))
  .subscribe(data => {
    this.data = data[0]; // extract the object
    console.log(this.data);
  })

现在您可以访问模板中对象的属性:

<table>
  <tbody>
    <tr>
      <td>ID:</td>
      <td>{{data?.id}}</td>
    </tr>
    <tr>
      <td>Date:</td>
      <td>{{data?.date}}</td>
    </tr>
   </tbody>
 </table>

【讨论】:

  • 谢谢。非常感谢你。它的工作方式很迷人。我只是注意到这是一个非常愚蠢的问题。谢谢您的回答。 :)
  • 不客气……正如他们所说,没有愚蠢的问题,只有愚蠢的答案;)嘿,有时我们都会犯最愚蠢的错误,至少我有过我的那份facepalm 时刻 :D 但很高兴我们把它解决了。祝您周末愉快,编码愉快! :)
【解决方案2】:

您的console.log()subscribe() 之外...调用beign 异步,this.data 尚未填充。

这应该有效:

 this.route.params
  .switchMap((params: Params) => this._dbService.get('MyAPI.php', 'id=' + params['id']))
  .subscribe((d:TbArr) => {
    this.data = d[0];
    console.log(this.data);
  })

在数据上放置async 管道没有意义,因为data 不是可观察的:

<td>{{data?.date | async}}</td> <!-- wrong ! -->
<td>{{data?.date}}</td> <!-- right ! -->

【讨论】:

  • 我根据您的建议修改了我的代码。但是只有控制台显示数据,我的网页仍然没有显示任何数据。你知道它为什么不显示吗?
猜你喜欢
  • 2016-11-10
  • 2020-01-28
  • 2021-11-23
  • 2017-11-20
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
  • 1970-01-01
  • 2016-10-05
相关资源
最近更新 更多