Ionic 2变量未显示在视图中

时间:2017-04-28 00:34:19

标签: angular ionic2

我在使用Angular 2时遇到了一些麻烦(我是新手)。我的Ionic应用程序无法识别我的阵列。每当我尝试从may arry或object访问属性时,离子2表示我的变量是空的。 这是我的代码: enter image description here 我的“观点”:

这是我的控制器showItem.ts:

enter image description here

2 个答案:

答案 0 :(得分:0)

尝试将您的图书类型设为any,看看您的数据是否已预先分配到预订位置:

book: any = [];

此外,当您将数据推送到书中时,请尝试使用this.book,而不是将this传递到函数中。

this.book.push({id:obj.id, title: obj.title, imagem: obj.imagem});

答案 1 :(得分:0)

问题出在我的提供者身上。实际上,这是一个我必须投入到一个对象中的错误。我的json正在返回一个对象内部的对象。我通过将res.json()转换为我的对象名称来解决这个问题:“book”。像这样:res.json()['book']


    buscarLivro(id)
    {
      if (this.dataBook) 
      {
        return Promise.resolve(this.dataBook);
      }
      // Dont have the data yet
      return new Promise(resolve => {
        this.http.get('http://localhost:8000/api/books/'+id)
          .map(res => res.json()['book'])
          .subscribe((data: any) => {
            this.dataBook = data;
            resolve( this.dataBook);
          });
      });
    }