【问题标题】:Issue with Json return object (Ionic) (Angularjs)Json 返回对象(Ionic)(Angularjs)的问题
【发布时间】:2017-05-09 15:52:49
【问题描述】:

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

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]});
}
}

【问题讨论】:

    标签: angularjs json ionic-framework


    【解决方案1】:

    attachments 是一个数组,所以你只能这样使用它,可能的解决方案是,

    <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>
    

    【讨论】:

    • 您好!,感谢您抽出宝贵时间发表评论,我也尝试过这种方式,仅显示 20 个结果中的 6 个,其余 14 个结果返回空白,没有标题和图像
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    相关资源
    最近更新 更多