标签:
使用g-class 动态设置元素的类,方法是绑定一个代表所有需要添加的类的表达式。重复的类不会添加。当表达式发生变化,先前添加的类会被移除,新类会被添加。
<!doctype html> <html ng-app="myApp"> <head> <link rel="stylesheet" href="//cdn.jsdelivr.net/foundation/4.3.2/css/foundation.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.js"></script> </head> <body> <div ng-controller="LotteryController"> <div ng-class="{red: x > 5}" ng-if="x > 5"> You won! </div> <button ng-click="x = generateNumber()" ng-init="x = 0">Draw Number</button> <p>Number is: {{ x }}</p> </div> </body> </html>
.red { background-color: red; }
angular.module(‘myApp‘, []) .controller(‘LotteryController‘, function($scope) { $scope.generateNumber = function() { return Math.floor((Math.random()*10)+1); }; });
标签:
原文地址:http://www.cnblogs.com/ByronWu12345/p/4853310.html