【问题标题】:Angularjs ng-repeat how do I get the object key name?Angularjs ng-repeat 如何获取对象键名?
【发布时间】:2015-12-23 16:57:07
【问题描述】:

我正在使用 Angularjs 搜索和显示存储在 documentDB 中的数据。我有一个使用 IHttpActionResult 提供数据的 Web Api 2 (.net)。

当我查看从 $http 返回的数据时,我可以看到结构与预期的一样,返回的文档的属性之一称为标识符,它是键值对类型场景中不同标识符的集合...见下文..

 "identifiers": {
  "sourceid": null,
  "secondaryid": null,
  "name": null,
  "anotherid1": null,
  "anotherid2": "412417118",
  "etc": null
},

显然,我的实际数据对子标识符有不同的名称,用上面的通用名称替换...

在我的 Html 中,我使用 ng-repeat 来显示每条记录的标识符列表,如下所示:

   <tbody>
                <tr>
                    <td >
                        <label>Identifiers: </label>
                    </td>
                    <td >
                        <p ng-repeat="ident in vessel.identifiers"> {{ident}}</p>
                    </td>
                    <td>
                        <label>Start Date: </label>
                    </td>
                    <td>
                        {{ vessel.startdate | date : 'dd/MM/yyyy'}}
                    </td>
                    <td>
                        <label>End Date: </label>
                    </td>
                    <td>
                        {{ vessel.enddate | date : 'dd/MM/yyyy'}}
                    </td>
                </tr>.........

但是,我写出的只是每个标识符的值,但没有键或任何东西……我尝试将其传递给控制器​​上的函数,但是当我这样做时 console.log 输入实际上是只是字符串值,不再是键/值对.....

如何让它写出key和value??

有趣的是,如果我去掉 ng-repeat 并且只是在那个 &lt;p&gt; 标记中使用 {{identifiers}},我会写出以下内容......

{"sourceid":null,"secondaryid":null,"name":null,"anotherid1":null,"anotherid2":"BJ5077","anotherid3":null,"etc":null,"etc2 ":""}

【问题讨论】:

    标签: angularjs-ng-repeat


    【解决方案1】:

    从 stackoverflow 帖子(链接here 以帮助其他人)中获得灵感后,这是我的 ng-repeat 的语法不正确,它应该看起来像这样......很明显你想想看!!!

     <p ng-repeat="(key, value) in vessel.identifiers"> {{key}} : {{value}} </p>
    

    【讨论】:

      猜你喜欢
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      • 2013-05-05
      • 2016-04-15
      • 1970-01-01
      相关资源
      最近更新 更多