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应用>>

posted @   Darren Ji  阅读(14993)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
历史上的今天:
2014-10-01 委托、Lambda表达式、事件系列04,委托链是怎样形成的, 多播委托, 调用委托链方法,委托链异常处理
2014-10-01 委托、Lambda表达式、事件系列03,从委托到Lamda表达式
2014-10-01 委托、Lambda表达式、事件系列02,什么时候该用委托
2014-10-01 委托、Lambda表达式、事件系列01,委托是什么,委托的基本用法,委托的Method和Target属性

我的公众号:新语新世界,欢迎关注。

点击右上角即可分享
微信分享提示