javascript - 当对象的某些字段丢失时如何使用 ng-repeat

标签 javascript css angularjs angularjs-ng-repeat

我正在一个对象上使用 Angular JS 的 ng-repeat。我的代码就像,

<thead>
     <tr>
        <th style="text-transform: capitalize;">{{monthOrquarter || "month"}}
        </th>
        <th ng-repeat="tech in dashboardSrv.portfolioTech">{{tech}}
        </th>
      </tr>
</thead>
<tbody>
     <tr ng-repeat="(month, data) in dashboardSrv.portfolio[monthOrquarter || 'month']">
       <td class="rt-td1" style="border-bottom: 1px solid white !important; border-top: 2px;">{{month}}
       </td>
       <td class="rt-td1" style="border-bottom: 1px solid white !important; border-top: 2px;" ng-repeat="(tech, percentage) in data" ng-show="data.tech != 'total'">{{percentage  | number: 2}}%
       </td>
     </tr>
 </tbody>

现在 dashboard.portfoliotech 看起来像, dashboard.portfolioTech = { '联系中心 EA', '联系中心 HCS','联系中心其他','远程专家'}

但是,dashboard.portfolio是一个看起来像的对象 Object structure

dashboardSrv中,var s=this 但在用户界面中,我想显示每个月/季度的这些(在每种技术下,相应的技术)。百分比值计算正确。月份/季度显示正确。但是,数据显示不正确(有时百分比采用了错误的技术)。我的用户界面看起来像, UI Screeshot

如何解决这个问题?

最佳答案

在最后一次重复中

ng-repeat="(tech, percentage) in data"

我建议重复投资组合技术。

ng-repeat="tech in dashboardSrv.portfolioTech"

并在您放置的绑定(bind)中

{{data[tech] | number: 2}}%

简而言之:

这一行:

<td class="rt-td1" style="border-bottom: 1px solid white !important; border-top: 2px;" ng-repeat="(tech, percentage) in data" ng-show="data.tech != 'total'">{{percentage  | number: 2}}%
   </td>

变成这一行:

<td class="rt-td1" style="border-bottom: 1px solid white !important; border-top: 2px;" ng-repeat="tech in dashboardSrv.portfolioTech" ng-show="data.tech != 'total'">{{data[tech]  | number: 2}}%
   </td>

关于javascript - 当对象的某些字段丢失时如何使用 ng-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42244935/

相关文章:

javascript - 如何将独特的可访问/可传递值烘焙到多个显示的按钮中?

javascript - watchify 和 gulp.watch 的区别

javascript - Jasmine 使用 requirejs 测试 knockout.js

php - 无法将 XMLHttpRequest.response 解析为对象或数组

javascript - Angular select 和 ng-model

javascript - 在 select onchange 上更改样式并停止返回到原始样式

css - Capybara - 如何检查提交按钮上的文本?

jquery - 仅初始页面加载动画

javascript - 通过手动输入的 ID 在 JSON 数组中查找

javascript - AngularJS:如何修改 document.onload 中的服务?