ngIf用于与Javascript Array不同的键类型

时间:2019-02-05 05:21:00

标签: arrays typescript angular6 javascript-objects

concerts: [
    {
        key: "field_concerts_time",
        lbl: "Date"
    }, {
        key: ["field_concert_fromtime", "field_concert_totime"],
        lbl: "Time",
        concat: "to"
    }, {
        key: "field_concerts_agereq",
        lbl: "Age Requirements"
    }, {
        key: "field_concerts_dresscode",
        lbl: "Dress Code"
    }, {
        key: "field_concerts_prices",
        lbl: "Prices"
    }

]

 descriptions: {

 "field_concerts_time": [

  {
  "value": "2019-09-16T00:00:00",
  }
  ],

"field_concert_totime": [

  {
  "value": "1:00AM"
  }

  ],

 "field_concert_fromtime": [

  {
  "value": "7:30PM"
  }

  ]
  }

我有Concerts数组和descriptions对象,我想从音乐会中获取“关键”并为该字段取值

    <p *ngFor="let otherdetail of otherdetailsarray">
    <span *ngIf="descriptions[otherdetail.key][0].value !== null"> 
      {{otherdetail.lbl}} :</span>
    <span *ngIf="descriptions[otherdetail.key][0].value !== null && 
      descriptions[otherdetail.key][0].value !== undefined"> 
      {{descriptions[otherdetail.key][0].value }}</span>
  </p>

我获得了第一个索引键“ field_concerts_time”的值,并使用上述标记从描述中获取了值。我希望这段代码支持"key: ["field_concert_fromtime", "field_concert_totime"]"

1 个答案:

答案 0 :(得分:1)

您在这里。检查您是否到达了音乐会的时间对象。 如果是这样,请通过另外索引字段0和1将键作为数组处理。

<p *ngFor="let otherdetail of otherdetailsarray">
<span *ngIf="descriptions[otherdetail.key][0].value !== null"> 
  {{otherdetail.lbl}} :</span>

<span *ngIf="otherdetail.lbl !== 'Time' &&
  descriptions[otherdetail.key][0].value !== null && 
  descriptions[otherdetail.key][0].value !== undefined">
  {{descriptions[otherdetail.key][0].value }} 
</span>

<span *ngIf="otherdetail.lbl == 'Time' &&
  descriptions[otherdetail.key[0]][0].value && 
  descriptions[otherdetail.key[1]][0].value"> 
   {{descriptions[otherdetail.key[0]][0].value }} {{otherdetail.concat}}
  {{descriptions[otherdetail.key[1]][0].value }}
  </span>

</p>

这应该可以解决问题。