【问题标题】:how to display the array inside json in ionic card如何在离子卡中显示json内部的数组
【发布时间】:2018-02-26 02:33:19
【问题描述】:

考虑下面的 json 数据

    [{
    "id":"1",
    "name": "abc",
    "url": "www.abc.com",
    "img":"abc.jpeg",
    "yield": "12",
    "ingredients":[
      "1 cup Rice flour<br/>",
      "2 cups Water<br/>",
      "1 teaspoon Cooking oil<br/>",
      "1/2 teaspoon Salt<br/>",
      "1 cup Fresh coconut , grated<br/>",
      "1/2 cup Jaggery<br/>",
      "1 teaspoon Cardamom Powder<br/>"
    ]
}]

现在,当我尝试在 ionic-card-content 中打印成分时,它显示为段落

 <ion-card-content class="item item-text-wrap">{{ recepie.ingredients }

o/p

1杯米粉,2杯水,1茶匙食用油,1/2茶匙 盐,1杯新鲜椰子,磨碎,1/2杯粗糖,1茶匙豆蔻 粉末

但我需要将这些成分打印在离子卡的每一行中。带分隔线

【问题讨论】:

标签: json angular ionic-framework ionic2


【解决方案1】:

您可以使用 *ngFor 指令,请参阅docs

<ion-card-content class="item item-text-wrap">
  <p *ngFor="let ingredient of recepie.ingredients">{{ ingredient }}</p>
</ion-card-content>

【讨论】:

  • 非常感谢您的帮助,我可以在这里添加一个分隔符吗?
  • 如果要添加更多元素,请将*ngFor 放入容器中(例如div),其所有子元素将针对数组的每个元素重复。 &lt;div *ngFor="let item of items"&gt;...&lt;/div&gt;
【解决方案2】:

关于你的json文件,因为你的json文件数据在数组中;

myData= [
  {
    "id":"1",
    "name": "abc",
    "url": "www.abc.com",
    "img":"abc.jpeg",
    "yield": "12",
    "ingredients":[
      "1 cup Rice flour<br/>",
      "2 cups Water<br/>",
      "1 teaspoon Cooking oil<br/>",
      "1/2 teaspoon Salt<br/>",
      "1 cup Fresh coconut , grated<br/>",
      "1/2 cup Jaggery<br/>",
      "1 teaspoon Cardamom Powder<br/>"
    ]
 },
 {
    "id":"2",
    "name": "xyz",
    "url": "www.xyz.com",
    "img":"xyz.jpeg",
    "yield": "12",
    "ingredients":[
      "3 cup Rice flour<br/>",
      "1 cups Water<br/>",
      "1 teaspoon Cooking oil<br/>",
      "3/2 teaspoon Salt<br/>",
      "2 cup Fresh coconut , grated<br/>",
      "1/2 cup Jaggery<br/>",
      "1 teaspoon Cardamom Powder<br/>"
    ]
 }
]

html代码一定是这样的

<ion-card-content class="item item-text-wrap" *ngFor= let item of myData>
  <p *ngFor="let ingredient of item.ingredients" class= >{{ ingredient }}</p>
</ion-card-content>

【讨论】:

    猜你喜欢
    • 2017-01-19
    • 2019-09-10
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 2022-11-30
    • 2018-05-08
    相关资源
    最近更新 更多