AngularJS ng-class

本文介绍了一个使用AngularJS实现动态样式的示例项目。通过定义控制器中的变量来控制元素的样式类,从而达到显示警告或错误信息的效果。示例中包含了一个简单的HTML页面和JavaScript代码,展示了如何根据不同条件更改元素背景色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

test.html 页面

<!DOCTYPE html>
<html ng-app>
<head>
	<title>Angular.js</title>
	<style type="text/css">
		.warning{
			background-color: yellow;
		}
		.error{
			background-color: red;
		}
	</style>
	<script type="text/javascript" src="angular-1.3.0.js"> </script>
	<script type="text/javascript" src="test.js"></script>
</head>
<body>
	<div ng-controller="WEController">
		<div ng-class="{error:isError,warning:isWarning}">{{messagetext}}</div>
		<button ng-click="error()">showError</button>
		<button ng-click="warn()">showWarn</button>
	</div>
</body>
</html>


 

test.js

function WEController($scope){
	$scope.isError = false;
	$scope.isWarning = false;

	$scope.warn = function(){
		$scope.messagetext = "this is warning text";
		$scope.isWarning = true;
		$scope.isError = false;
	};

	$scope.error = function(){
		$scope.messagetext = "this is error text";
		$scope.isWarning = false;
		$scope.isError = true;
	}
}
初始画面


点击showError



点击showWarn



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值