时间:2021-05-25
整理文档,搜刮出一个详解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>以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了AngularJS使用ng-class动态增减class样式的方法。分享给大家供大家参考,具体如下:使用ng-class可以实现动态地增减样式:a
首先设定一段Angularjs代码的ng-class:比较容易理解的Angularjsng-class设置样式代码,那我们使用React怎么去实现它呢?首先在s
本文实例讲述了AngularJS中ng-class用法。分享给大家供大家参考,具体如下:使用ng-class动态设置元素的类,方法是绑定一个代表所有需要添加的类
之前用三目表达式和ng-class实现了按钮切换效果,似乎达到了我的预期,但是我觉得还有改进空间,网上找了一些资料,大概还有以下几种实现方式:路由btn1btn
AngularJS事件AngularJS有自己的HTML事件指令。ng-click指令ng-click指令定义了AngularJS点击事件。AngularJS实