原文地址:http://www.cnblogs.com/pilixiami/p/5723550.html
Rating是一个用于打分或排名的控件。看一个最简单的例子:
<!DOCTYPE html> <html ng-app="ui.bootstrap.demo" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="/Content/bootstrap.css" rel="stylesheet" /> <title></title> <script src="/Scripts/angular.js"></script> <script src="/Scripts/ui-bootstrap-tpls-1.3.2.js"></script> <script> angular.module('ui.bootstrap.demo', ['ui.bootstrap']).controller('RatingDemoCtrl', function ($scope) { $scope.rate = 7; $scope.max = 10; $scope.isReadonly = false; $scope.hoveringOver = function (value) { $scope.overStar = value; $scope.percent = 100 * (value / $scope.max); }; }); </script> </head> <body style="padding:10px"> <div ng-controller="RatingDemoCtrl"> <uib-rating ng-model="rate" max="max" read-only="isReadonly" on-hover="hoveringOver(value)" on-leave="overStar = null" titles="['one','two','three']" ></uib-rating> <span class="label" ng-class="{'label-warning': percent<30, 'label-info': percent>=30 && percent<70, 'label-success': percent>=70}" ng-show="overStar && !isReadonly">{{percent}}%</span> </div> </body> </html>
效果是这样:
uib-rating可以使用的属性有:
属性名 | 默认值 | 备注 |
max | 5 | 图标的最大个数 |
ng-model | 当前图标数量 | |
on-hover(value) | 一个可选的表达式(函数),当鼠标放在图标上时触发 | |
on-leave() | ||
rating-states | null | 一个数组,定义所有图标的属性。默认的模板中,使用state-on和state-off定义图标的类名 |
read-only | false | 是否只读 |
titles | ['one', 'two', 'three', 'four', 'five'] | 一个字符串数组,定义所有图标的标题(鼠标悬停在图标上时会显示标题) |
enable-reset | true | 点击当前分数的图标会将分数重置为0 |
state-off | null | 一个变量,表示未选中图标的状态 |
state-on | null | 一个变量,表示选中了的图标的状态 |
这些属性中,rating-states、state-on和state-off可以自定义图标的类名
例如,使用rating-states为每个图标分别设置选中和未选中的类名
$scope.ratingStates = [
{ stateOn: 'glyphicon-ok-sign', stateOff: 'glyphicon-ok-circle' },
{ stateOn: 'glyphicon-star', stateOff: 'glyphicon-star-empty' },
{ stateOn: 'glyphicon-heart', stateOff: 'glyphicon-ban-circle' },
{ stateOn: 'glyphicon-heart' },
{ stateOff: 'glyphicon-off' }
];
或者不使用rating-states,而使用state-on和state-off定义所有图标的类:
<div ng-controller="RatingDemoCtrl"> <uib-rating ng-model="rate" max="max" on-hover="hoveringOver(value)" on-leave="overStar = null" state-on="'glyphicon-ok-sign'" state-off="'glyphicon-ok-circle'" > </uib-rating> </div>
用uibRatingConfig可以设置Rating的全局属性。如:
angular.module('ui.bootstrap.demo', ['ui.bootstrap'])
.config(['uibRatingConfig', function (uibRatingConfig) {
uibRatingConfig.max = 3;
uibRatingConfig.stateOn = 'glyphicon-ok-sign';
uibRatingConfig.stateOff = 'glyphicon-ok-circle';
}])
.controller('RatingDemoCtrl', function ($scope) {
$scope.rate = 2;
});
默认的全局属性是:
{
max: 5,
stateOn: null,
stateOff: null,
enableReset: true,
titles : ['one', 'two', 'three', 'four', 'five']
}