gpt4 book ai didi

javascript - 在angularjs中内联If else

转载 作者:行者123 更新时间:2023-11-30 20:26:06 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com