作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
<div class="alcohol">
<img src="~/img/Interaction/alcohol.png" title="Alcohol" />
<span>{{product.interaction}}</span>
</div>
IF {{product.interaction}} == 'CAUTION'
表示 Span 颜色是红色你知道吗??
最佳答案
将 ng-style
与条件(三元)运算符一起使用,该运算符检查某些字符串并给出特定样式。这是它的样子:
<span ng-style="product.interaction=='CAUTION' ? {color:'red'} : ''">
{{product.interaction}}
</span>
(对于else,添加白色)
简单地扩展else子句
<span ng-style="product.interaction=='CAUTION' ? {color:'red'} : {color:'white'}">
{{product.interaction}}
</span>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.product = {};
$scope.product.interaction = "CAUTION";
$scope.product.interaction2 = "ANYTHING"; // to show `else` variation
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<div class="alcohol">
<span ng-style="product.interaction2=='CAUTION' ? {color:'red'} : {color:'white'}">
{{product.interaction2}}
</span>
<span ng-style="product.interaction=='CAUTION' ? {color:'red'} : {color:'white'}">
{{product.interaction}}
</span>
</div>
</div>
</body>
</html>
关于javascript - 在angularjs中内联If else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50910207/
我是一名优秀的程序员,十分优秀!