javascript - 如何修复类名而不是 ng 类名

标签 javascript css angularjs

<分区>


想改进这个问题吗? 通过 editing this post 添加细节并澄清问题.

关闭 6 年前

<div ng-class:"{{myclass}}" role="progressbar" aria-valuenow="{{roundedtotalPerformanceCount}}" 
     aria-valuemin="0" aria-valuemax="100" ng-style="{'width' : ( totalPerformanceCount + '%' ) }">
     {{roundedtotalPerformanceCount}}&nbsp;%
</div>

我的 Controller 代码是

if ($scope.roundedtotalPerformanceCount <= 20)
{
   $scope.myclass='progress-bar-below20';
}
else
{
   $scope.myclass = 'progress-bar';
}

最佳答案

以这种方式更改您的 ng-class,您可以将表达式放入:

ng-class="{'progress-bar-below20': roundedtotalPerformanceCount <= 20, 'progress-bar': roundedtotalPerformanceCount > 20}"

你也有错别字。有 : ,应该是=

这是错误的:

<div ng-class:"{{myclass}}"

应该是:

<div ng-class="myclass"

为了提高可读性,我的建议是将带有检查表达式的函数放入其中:

ng-class="{'progress-bar-below20': checkRoundedtotalPerformanceCount(), 'progress-bar': !checkRoundedtotalPerformanceCount()}"

或者如果你喜欢使用三元运算符:

ng-class="checkRoundedtotalPerformanceCount() ? 'progress-bar-below20': 'progress-bar'"

关于javascript - 如何修复类名而不是 ng 类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44156847/

上一篇:javascript - 如何在php中将按钮id从一个页面传递到另一个页面

下一篇:javascript - 页面/表格加载动画

相关文章:

javascript - 有人可以解释一下这段代码是由什么组成的吗?

jquery - 向用户展示大量数据的有效方法有哪些?

javascript - 拖放 Javascript if 语句问题

angularjs - Angular.module 默认名称

javascript - AngularJS - 为什么不替换 : true work with templateUrl property?

angularjs - 用于放置/删除的 CORS Cakephp 3.0 预检失败

javascript - 如果数组的连续元素重复,则通过保留该值的一个实例来仅过滤掉该数组

javascript - 如何有条件地在数组中添加或删除元素

javascript - 调整浏览器大小时jquery div innerheight不更新

html - 我怎样才能让我的导航粘在页面顶部?