AngularJS动态设置CSS

 

使用AngularJS动态设置CSS大致有2种思路:

 

1、通过动态设置class名称

 

比如先定义2个样式:

 

.show-true{
    display:block;
}

.show-flase{
    display:none;
}

 

在某个元素中:

<div class="show-{{temp}}".....

 

temp为$scope的一个变量,通过设置$scope.temp = true 或 $scope.temp = false来决定temp变量的具体值。

 

比如:

 

 
 
<!doctype html>
<html ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
    <style>
        .menu-disabled-true{
            color: gray;
        }
        
        .menu-disabled-false{
            color: red;
        }
    </style>
    <script src="angular.min.js"></script>
    <script>
        var myApp = angular.module("myApp",[]);
        myApp.controller("MyController", function($scope){
            $scope.isDisabled = false;
            $scope.changeState = function(){
                $scope.isDisabled = true;
            };
        });
    </script>
</head>
<body ng-controller="MyController">
    <ul>
        <li class="menu-disabled-{{isDisabled}}" ng-click="changeState()">hello</li>
    </ul>
</body>
</html>

 

2、使用ng-class

 

ng-class显示一个对象,比如ng-class="{selected: true}"表示class="selected"。

 

以下ng-class的字段直接接收bool值。

 

 
 
<!doctype html>
<html ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
    <style>
        .error{
            background-color: red;
        }
        
        .warning{
            background-color: yellow;
        }
    </style>
    <script src="angular.min.js"></script>
    <script>
        var myApp = angular.module("myApp",[]);
        myApp.controller("MyController",function($scope){
            $scope.isError = false;
            $scope.isWarning = false;
            
            $scope.showError = function(){
                $scope.messageText = "error";
                $scope.isError = true;
                $scope.isWarning = false;
            };
            
            $scope.showWarning = function(){
                $scope.messageText = "warning";
                $scope.isError = false;
                $scope.isWarning = true;
            };
        });
    </script>
</head>
<body ng-controller="MyController">
 
 
<div ng-class="{error:isError, warning:isWarning}">{{messageText}}</div>
<button ng-click="showError()">显示error</button>
<button ng-click="showWarning()">显示warning</button>
 
 
</body>
</html>
 
 
 
 

 

以下,ng-class的字段接收一个返回bool类型的表达式。

 

 
 
<!doctype html>
<html ng-app="myApp">
<head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
    <style>
        .selected{
            background-color: lightgreen;
        }
    </style>
    <script src="angular.min.js"></script>
    <script>
        
        
        var myApp = angular.module("myApp",[]);
        myApp.controller("MyController",function($scope){
            $scope.person =[
                {name: 'darren', age: '20'},
                {name: 'jack', age: '23'}
            ];
            
            $scope.selectPerson = function(rowIndex){
                $scope.selectedRow = rowIndex;
            };
        });
    </script>
</head>
<body>
 
 
<table ng-controller="MyController">
    <tr ng-repeat="p in person" ng-click="selectPerson($index)" ng-class="{selected: $index==selectedRow}">
        <td>{{p.name}}</td>
        <td>{{p.age}}</td>
    </tr>
</table>
 
 
</body>
</html>
 
 
 
 

 

参考资料:<<用AngularJS开发下一代Web应用>>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值