【问题标题】:ngIf for different type of key from Javascript ArrayngIf 用于 Javascript 数组中不同类型的键
【发布时间】:2019-06-28 21:33:36
【问题描述】:
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"
  }

  ]
  }

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

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

【问题讨论】:

    标签: arrays typescript angular6 javascript-objects


    【解决方案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>
    

    这应该可以解决问题。

    【讨论】:

    • 抱歉,由于出现拼写错误,我不得不再次更正我的代码。现在应该可以工作了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 2013-04-25
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多