简体   繁体   English

如何使用Angular.js在单个类中添加多个条件

[英]How to add multiple condition in a single class using Angular.js

I need one help. 我需要一个帮助。 I need to add multiple condition with AND operator in ng-class using Angular.js. 我需要使用Angular.js在ng-class使用AND运算符添加多个条件。 I am explaining my code below. 我在下面解释我的代码。

<div class="mainaccordion_title-width" ng-click="manageCollapseExpand(place, false)">
  <i ng-class="{'fa fa-minus-circle': place.expanded, 'fa fa-plus-circle': !place.expanded }"></i> 
  {{place.root_des}}
</div>

Here I am adding one condition but I need to add another condition ( ie-place.cstatus==1 and place.cstatus==0 ) for both class. 这里我添加一个条件,但我需要为这两个类添加另一个条件( ie-place.cstatus==1 and place.cstatus==0 )。 Please help. 请帮忙。

使用&&而不是

(i.e-place.cstatus==1 && place.cstatus==0)

As commented before, you can also create a function that takes arguments and return classnames. 如前所述,您还可以创建一个带参数和返回类名的函数。

Benefit of this approach is, it will keep your view clean and the logic will stay in JS. 这种方法的好处是,它将保持您的视图清洁,逻辑将保留在JS中。 This will also make it more configurable as you can write a code and share it across multiple elements. 这也使它更易于配置,因为您可以编写代码并在多个元素之间共享它。

 function myCtrl($scope) { $scope.place = { expanded: false, cstatus: 1, root_des: 'test' } $scope.manageCollapseExpand = function(place, seleccted) { place.expanded = !place.expanded } // assuming place should be an array $scope.getClasses = function(index) { var className = 'fa '; if ($scope.place.expanded && $scope.place.cstatus === 1) { className += 'fa-minus-circle ' } else { className += 'fa-plus-circle '; } return className } } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app ng-controller='myCtrl'> <div class="mainaccordion_title-width" ng-click="manageCollapseExpand(place, false)"> <i ng-class="getClasses(0)"></i> {{place.root_des}} <p>Classes: {{getClasses(0)}}</p> </div> </div> 

Simply you can conbind condition using && 只需使用&&即可解决条件

<div class="mainaccordion_title-width" ng-click="manageCollapseExpand(place, false)">
  <i ng-class="{'fa fa-minus-circle': place.expanded && place.cstatus==1, 'fa fa-plus-circle': !place.expanded && place.cstatus==0 }"></i> 
  {{place.root_des}}
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM