【问题标题】:How to display an array of object without knowing the data model如何在不知道数据模型的情况下显示对象数组
【发布时间】:2018-07-29 21:24:58
【问题描述】:

我有一个对象数组,这是我的对象结构:

[{
  "_index": "bank",
  "_type": "account",
  "_id": "25",
  "_score": 1,
  "_source": {
    "account_number": 25,
    "balance": 40540,
    "firstname": "Virginia",
    "lastname": "Ayala",
    "age": 39
  }
}, ... ]

我需要去掉_index_type_id_score(只显示_source) 我想在每个键之前显示一个包含值的输入:

account_number[25]

余额[40540]......

<tbody>
    <tr ng-repeat="row in ctrl_hits.data | limitTo: row._index+row._type+row._id+row._score">
      <td> {{row.key}} : 
          <input type="text"
          ng-value="row.value">
      </td>
    </tr>
</tbody>

NW:我知道,我正在编写愚蠢的代码,只是为了解释我的需求。

【问题讨论】:

    标签: angularjs angularjs-ng-repeat


    【解决方案1】:

    ng-repeat="(key,value) in row._source":

    <tbody>
        <tr ng-repeat="row in ctrl_hits.data | limitTo: row._index+row._type+row._id+row._score">
          <td ng-repeat="(key,value) in row._source">
              {{key}} : 
              <input type="text" ng-model="row._source[key]">
          </td>
        </tr>
    </tbody>
    

    【讨论】:

      【解决方案2】:

      首先你必须对数组使用map函数来消除无用数据(_index,_type等)并保留_source。

      如果 x 是您的对象数组,则 ctrl_hits.data 将拥有一个包含您之前数据的 _source 对象的数组。

      ctrl_hits.data=x.map(function(e){
          return e._source;
      })
      

      然后在你的 html 中

       <tbody>
              <tr ng-repeat="item in ctrl_hits.data">
                <td ng-repeat=(key,value) in item>              
                  {{key}} : <input type="text" ng-model="value"/>
                </td>
              </tr>
       </tbody>
      

      【讨论】:

        猜你喜欢
        • 2016-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-05
        • 2016-07-21
        • 1970-01-01
        相关资源
        最近更新 更多