代码之家  ›  专栏  ›  技术社区  ›  Ivin Raj

在AngularJS中内联if else

  •  1
  • Ivin Raj  · 技术社区  · 7 年前
    <div class="alcohol">
           <img src="~/img/Interaction/alcohol.png" title="Alcohol" />
           <span>{{product.interaction}}</span>
      </div>
    

    如果 {{product.interaction}} == 'CAUTION' 意思是跨距颜色是红色,知道吗?是吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Aleksey Solovey    7 年前

    ng-style 有条件的( )用于检查某些字符串并给出特定样式的运算符。这是它的样子:

    <span ng-style="product.interaction=='CAUTION' ? {color:'red'} : ''">
      {{product.interaction}}
    </span>
    

    其他的

    只需扩展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>