html - 使用 AngularJS 更改 CSS 取决于文本框值

标签 html css angularjs

我有两个输入数值的文本框。

<input type="text" id="perse1" ng-init="perse1=0" ng-model="perse1" />
<input type="text" id="perse2" ng-init="perse2=0" ng-model="perse2" />

我有一个计算总和的标签

Total value is <label>{{(parse1*1) + (parse2*1)}}</label>

现在我想在这个标签上应用不同的 css。当总值等于 100 时,则 .alert-success 否则 .alert-warning

我尝试了以下代码,但没有任何效果。

<label ng-model="total" ng-class="'alert-success': total.text==100,'alert-warning': total.text!=100">{{(parse1*1) + (parse2*1)}}</label>

最佳答案

首先,修正拼写错误(parse1 != perse1 等)。

然后你可以在ng-class中重复计算(避免引入另一个模型):

<label ng-class="{'alert-success': perse1 + perse2==100,'alert-warning': perse1 + perse2 !=100}">
    {{(perse1*1) + (perse2*1)}}
</label>

我没有包括乘以 1,但可以随意添加它。


这是一个完整的示例:

.alert-success {
  color: green;
  }

.alert-warning {
  color: red;
  }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<input type="number" id="perse1" ng-init="perse1=0" ng-model="perse1" />
<input type="number" id="perse2" ng-init="perse2=0" ng-model="perse2" />

<label ng-class="{'alert-success': (perse1 + perse2==100),'alert-warning': (perse1 + perse2 !=100)}">
    {{perse1 + perse2}}
</label>

关于html - 使用 AngularJS 更改 CSS 取决于文本框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27813838/

相关文章:

html - 如何设置表单格式以便它们垂直对齐?

AngularJS:如何提供搜索工具并导致传递参数的路由更改?

javascript - 如何将元素 ID 变量传递给 jQuery 加载函数

JavaScript 滚动条看起来不像它应该的样子

html - 如何绝对定位一个相对定位的元素?

html - CSS div 位于一排并带有 Overflow-x 滚动

javascript - 我希望表格具有隐藏的输入字段,仅当我单击它们时才会出现

javascript - angular.js 从数组中获取特定值

javascript - 使用 MixItUp 在一个网格中进行多种排序

html - post JSF中的字符编码乱码