简体   繁体   English

如何在缺少对象的某些字段时使用ng-repeat

[英]how to use ng-repeat when some fields of the object are missing

I am using ng-repeat of Angular JS on an object. 我在一个对象上使用Ang-JS的ng-repeat。 My code is like, 我的代码就像,

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

Now dashboard.portfoliotech looks like, dashboard.portfolioTech = { 'Contact Center EA', 'Contact Center HCS','Contact Center Other','Remote Expert'} 现在dashboard.portfoliotech看起来像dashboard.portfolioTech = { 'Contact Center EA', 'Contact Center HCS','Contact Center Other','Remote Expert'}

But, dashboard.portfolio is an object which looks like 但是, dashboard.portfolio是一个看起来像的对象 对象结构

In dashboardSrv , var s=this But in UI I want to show these (under each technology, the corresponding technology) for each Month/ Quarter. dashboardSrvvar s=this但是在UI中我想为每个月/季度显示这些(在每种技术下,相应的技术)。 Percentage values are computed correctly. 百分比值是正确计算的。 Month/ Quarters are shown correctly. 月/季度显示正确。 But, data is not shown correctly (sometimes the % goes under wrong technology). 但是,数据没有正确显示(有时%是错误的技术)。 My UI looks like, 我的UI看起来像, UI Screeshot

How to fix that? 如何解决?

In your last repeat 在你上次重复

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

I suggest repeating over portfolioTech instead. 我建议重复一下portfolioTech。

ng-repeat="tech in dashboardSrv.portfolioTech"

and in the binding that follows you place 并在你跟随的绑定中

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

In short: 简而言之:

This line: 这一行:

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

becomes this line: 成为这一行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM