【问题标题】:Conditions and loops withing AngularJS expressions?带有AngularJS表达式的条件和循环?
【发布时间】:2016-05-14 06:10:00
【问题描述】:

我有一组 JSON 数据,包含以下两个对象:

{  
   "date":"20160118",
   "entity":"01",
   "security":{  
      "securityID":"191457",
      "identifiers":[  
         {  
            "value":"345342532",
            "type":"fii"
     },
     {  
           "value":"32453452",
           "type":"isin"
     },
     {  
           "value":"48125D886",
           "type":"cusip"
     }
    ]
   }
}

还有:

   {
   "date":"342534543",
   "entity":"01",
   "security":{  
      "securityID":"3425435",
      "identifiers":[  
       {  
          "value":"32543543",
          "type":"fii"
       }
     ]
   }
  }

我正在创建一个只使用 AngularJS 表达式和 HTML 的表格。

我在访问security.identifiers[#].value 时遇到了麻烦。根据证券有多少identifiers - 它们可能位于不同的数组位置 - 这意味着“fii”可能最终与前一行的“cusip”在同一列中。在我的角度表达式中是否有一种方法可以搜索数组并找到一个字符串?在这种情况下,identifiers.type 将是了解它放置在哪一列中的关键。我尝试在我的 javascript 中循环并为我的表达式提供索引,但我似乎遇到了关闭问题 - 我很高兴有一个更简单的方法。

到目前为止,我的 identifiers 专栏有这个 - 我知道它们是错误的,但希望能让您了解我正在尝试做什么。

            <td>{{data.security.identifiers.type === "fii" ? data.security.identifiers.value : ""}}</td>
            <td>{{data.security.identifiers.type === "isin" ? data.security.identifiers.value : ""}}</td>
            <td>{{data.security.identifiers.type === "cusip" ? data.security.identifiers.value : ""}}</td>

【问题讨论】:

  • 你能在 plnk 的某个地方提供完整的例子吗?
  • @STEVER plnkr.co/edit/OWP8krJBUmZczsqHwyW9?p=preview 好的,这基本上就是我现在的位置。如您所见,第 1 行的 cusip 值显示在 fii 列中,因为 fii 的数组位置是 0,因为没有其他标识符。

标签: javascript html angularjs html-table conditional


【解决方案1】:

我能理解你想要做什么,下面的代码可以像你期望的那样工作。

<td>{{data.security.identifiers.filter(function(v){return v.type == 'fii'})[0].value}}</td>

为了改进你可以创建一个函数来做过滤器(可能在控制器或服务中):

$scope.identifierFilter = function(type) {
    var filtered = this.data.security.identifiers.filter(function(v) {
        return v.type == type;
    });
    return filtered && filtered[0] ? filtered[0].value : '';
};

在html中

<td>{{identifierFilter('fii')}}</td>

我在看你的例子,我认为上面的代码可以工作。但是如果你可以改变你的 JSON 结构,它会更好更容易。

"identifiers":{
    fii  : 'dfdfdf',
    isin : '32453452',
    cusip: '48125D886'
}

html可以简单的喜欢

<td>{{data.security.identifiers.fii}}</td>

如果你想做这个转换,我可以给你进一步的答案。

可以帮忙


我更新了您的示例,对于上述代码的一些语法错误,我们深表歉意。

请查看:https://plnkr.co/edit/NgvJKrfOGmKHfOap08xU?p=preview


这是模板使用前的重构数据。如果数据结构可以改变,选择你喜欢的更好。

https://plnkr.co/edit/7eHAvSaBcOiTajOomNEs?p=preview

【讨论】:

  • 感谢您的输入,但我不确定第一个选项如何工作,考虑到 fiicusipisin 可能位于数组位置 [0]、@987654333 @ 或 [2]?
  • 它不是用index过滤标识符值,而是直接搜索type属性。你可能对[0]混淆了,因为过滤方法结果是一个数组,我们只是想要第一个对象结果。您可以在代码中对其进行测试
  • Array.prototype.filter方法api可以在:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…循环数组,找到type = 'fii'
  • 基于过滤器的解决方案很聪明,但会出现性能问题(过滤器在每个 $digest 循环上运行,请记住)。重构控制器中的数据是一个更好的解决方案。
  • 是的,我想我会试试的。我个人无法获得上述任何一个选项。
【解决方案2】:

您最好的选择可能是使用指令,或者根据您的要求进行过滤。指令将使您的标记清晰,您将封装您需要的功能。例如。

<table border="1" style="width:100%">
  <tr>
    <th>Entity</th>
    <th>SecurityID</th>
    <th>Security1</th>
    <th>Security2</th>
    <th>Security3</th>
    <th>date</th>
  </tr>
  <tr ng-repeat="data in JSONData">
    <td>{{data.entity}}</td>
    <td>{{data.security.securityID}}</td>
    <td><identifier identifiers="data.security.identifiers" render-type="fii"></identifier></td>
    <td><identifier identifiers="data.security.identifiers" render-type="isin"></identifier></td>
    <td><identifier identifiers="data.security.identifiers" render-type="cusip"></identifier></td>
    <td>{{data.date}}</td>
  </tr>
</table>

笨蛋: https://plnkr.co/edit/6VJuQob1jfDzsR8XPKKM?p=preview

希望这会有所帮助。

【讨论】:

  • 谢谢,我已经接受了果冻的回答,因为它现在正在工作,但是当我明天重新上班时,一定会看看这个。
  • @Amonn 好主意。但它有一个限制,当原点data 改变时,视图也不能自动改变。
  • 是的,您必须在指令中为要监控的每个数据值设置 $watch,并做出相应的响应。
猜你喜欢
  • 2013-10-22
  • 2021-09-02
  • 2020-10-05
  • 2014-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-25
相关资源
最近更新 更多