详解AngularJS ng-class样式切换

六月下雨了,天又下了雨。我已经很久没有见过如此下雨了,柔软而缠绵,就像你的眼睛,轻轻地刷着我寂寞的肩膀。闻一闻书本,站在窗前,对朋友说:早上好!

整理文档,搜刮出一个详解AngularJS ng-class样式切换,稍微整理精简一下做下分享。

1、HTML

<ion-view> 
  <ion-content> 
    <div class="button-bar"> 
      <div ng-class="{true: 'bgstyle-check', false: 'bgstyle'}[isFirst]" ng-click="isFirst = !isFirst">First</div> 
      <div ng-class="{true: 'bgstyle-check', false: 'bgstyle'}[isSecond]" ng-click="isSecond = !isSecond">Second</div> 
      <div ng-class="{true: 'bgstyle-check', false: 'bgstyle'}[isThird]" ng-click="isThird = !isThird">Third</div> 
    </div> 
    <br><br> 
    <div class="button-bar"> 
      <div ng-class="{true: 'bgstyle-check', false: 'bgstyle'}[hasAll]" ng-click="toggleAll()">All</div> 
      <div ng-class="{true: 'bgstyle-check', false: 'bgstyle'}[hasFirst]" ng-click="toggleFirst()">First</div> 
      <div ng-class="{true: 'bgstyle-check', false: 'bgstyle'}[hasSecond]" ng-click="toggleSecond()">Second</div> 
      <div ng-class="{true: 'bgstyle-check', false: 'bgstyle'}[hasThird]" ng-click="toggleThird()">Third</div> 
    </div> 
  </ion-content> 
</ion-view> 
<style> 
  .bgstyle {background-color: #8f8f8f;width: 100%;height: 20px;margin: 2px 0 2px 0;text-align: center;} 
  .bgstyle-check {background-color: #ff3b30;width: 100%;height: 20px;margin: 2px 0 2px 0;text-align: center;} 
</style> 

2、controller

appControllers.controller('TestlCtrl', function ($scope, $state) { 
  $scope.isFirst = false; 
  $scope.isSecond = false; 
  $scope.isThird = false; 
 
 
  $scope.hasAll = false; 
  $scope.hasFirst = false; 
  $scope.hasSecond = false; 
  $scope.hasThird = false; 
 
  $scope.toggleAll = function () { 
    $scope.hasAll = !$scope.hasAll; 
    console.log($scope.hasAll); 
    var dynamicValue = $scope.hasAll; 
    $scope.hasFirst = dynamicValue; 
    $scope.hasSecond = dynamicValue; 
    $scope.hasThird = dynamicValue; 
  } 
 
  $scope.toggleFirst = function () { 
    $scope.hasFirst = !$scope.hasFirst; 
    checkAll(); 
  } 
 
  $scope.toggleSecond = function () { 
    $scope.hasSecond = !$scope.hasSecond; 
    checkAll(); 
  } 
 
  $scope.toggleThird = function () { 
    $scope.hasThird = !$scope.hasThird; 
    checkAll(); 
  } 
 
  function checkAll() { 
    if ($scope.hasFirst == true && $scope.hasSecond == true && $scope.hasThird == true) { 
      console.log("123ok"); 
      $scope.hasAll = true; 
    } else { 
      console.log("123no"); 
      $scope.hasAll = false; 
    } 
 
  } 
}) 


3、效果图

4、循环列表,判断索引添加样式

<div class="category-tab "> 
  <ul> 
    <li ng-repeat="item in rootList" ng-class="{true: 'cur', false: ''}[$index+1===1]"> 
      <a rel="nofollow noopener noreferrer" href="">{{item.CategoryName}}</a> 
    </li> 
    <li><a rel="nofollow noopener noreferrer" href="">热门推荐</a></li> 
    <li><a rel="nofollow noopener noreferrer" href="">热门推荐</a> </li> 
  </ul> 
</div> 

*、

<ion-item class="item-divider"> 
  <i ng-class="{true: 'icon ion-tips mr10', false: 'iconfont icon-shangdian text-orange mr10'}[item.VendorId==0]"> 
    <element ng-show="item.VendorId==0">合作</element></i>{{item.VendorName}} 
</ion-item> 

以上就是详解AngularJS ng-class样式切换。最完美的状态就是,看过了世界的黑暗与痛苦,却依然坚信它的单纯与完美。期望那时候,咱们都变成了更好的自我。没有早一步,也没有晚一步的,就在那时候,遇到你。更多关于详解AngularJS ng-class样式切换请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
AngularJS tab栏实现和mvc小案例实例详解

基于AngularJS拖拽插件ngDraggable.js实现拖拽排序功能

AngularJS实现的自定义过滤器简单示例

ionic使用angularjs表单验证(模板验证)

用WebStorm进行Angularjs 2开发(环境篇:Windows 10,Angular-cli方式)