详解AngularJS ng-class样式切换
程序员文章站
2023-02-24 10:53:24
整理文档,搜刮出一个详解angularjs ng-class样式切换,稍微整理精简一下做下分享。
1、html
<...
整理文档,搜刮出一个详解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 href="">{{item.categoryname}}</a> </li> <li><a href="">热门推荐</a></li> <li><a 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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。