【发布时间】:2023-03-30 12:15:03
【问题描述】:
我有以下代码,效果很好:
<div class="progress progress-striped active">
<div class="progress-bar progress-bar-success" style="width: {{(p.availableIpAddressCount/p.totalIpAddressCount)*100|number:0}}%;background-color: #5cb85c">
{{(p.availableIpAddressCount/p.totalIpAddressCount)*100|number:0}}
</div>
</div>
但是,如您所见,我重复了很多 {{(p.availableIpAddressCount/p.totalIpAddressCount)*100|number:0}},因此我想将其分配给角度变量。我还想对结果进行 NG-switch。这是我尝试过的
<div class="progress progress-striped active" ng-init = "barPercentage= {{(p.availableIpAddressCount/p.totalIpAddressCount)*100|number:0}}">
<div ng-switch="barPercentage">
<div ng-switch-when=">=0" class="progress-bar progress-bar-success" style="width: {{barPercentage}}%;background-color: #5cb85c">
{{(p.availableIpAddressCount/p.totalIpAddressCount)*100|number:0}}
</div>
</div>
</div>
但这根本不起作用,但我不确定为什么。我在控制台中没有收到任何错误。
有什么想法吗?
【问题讨论】:
-
只使用 ng-if 怎么样
-
你不需要插值,即
ng-init = "barPercentage=(p.availableIpAddressCount/p.totalIpAddressCount)*100|number:0" -
@Satpal 如果没有插值,它仍然无法工作。谢谢你的建议。
-
@MMK 这没什么区别,谢谢。
-
@MattBoyl etry 在 ng-int 中避免
|number:0并尝试排除{{}}。工作样本在这里jsfiddle.net/U3pVM/29446。如果有任何问题,请让我更新
标签: angularjs angularjs-ng-repeat angular-ng-if ng-switch