Json返回对象(Ionic)的问题(Angularjs)

时间:2017-05-09 15:52:49

标签: angularjs json ionic-framework

我正在尝试迭代所有json文件,它与{{title}}一起使用并显示所有内容,但它不适用于附件网址。

的Json

LINK IMAGE JSON

HTML

  <ion-content>      
    <ion-card *ngFor="let user of users">
    <img [src]="user.attachments.url"> <!--It doesn't work-->
    <ion-card-content>
    <ion-card-title>
    {{ user.title }} <!--WORK-->
    </ion-card-title>
  </ion-content>

提供商

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';


@Injectable()
export class UserService {
  data: any;

  constructor(private http: Http) {
    this.data = null;
  }

  load() {
    if (this.data) {
      // already loaded data
      return Promise.resolve(this.data);
    }

    // don't have the data yet
    return new Promise(resolve => {
      this.http.get('file.json')
        .map(res => res.json())
        .subscribe(data => {
          this.data = data.posts;
          resolve(this.data);
        });
    });


  }
      generateArray(obj){
   return Object.keys(obj).map((key)=>{ return obj[key]});
}
}

1 个答案:

答案 0 :(得分:1)

附件是一个数组,所以你必须只使用它,可能的解决方案是,

<ion-content>      
<ion-card *ngFor="let user of users">
<img [src]="user.attachments[0].url"> <!--first attachment. It will give error for no attachments-->
<ion-card-content>
<ion-card-title>
{{ user.title }} <!--WORK-->
</ion-card-title>