【发布时间】:2017-04-27 07:01:10
【问题描述】:
我有这个星级演示。在这里,星星显示为图像。
我想使用字体很棒的图标或 css 来表示星星。谁能帮我吗?附上代码。请查看并告诉我。如果有人有更简单的方法,请发布您的答案。我还需要在悬停或点击星星时显示一些消息。
'use strict';
var app = angular.module('app', []);
app.controller('appController', ['$scope', function ($scope) {
$scope.starRating1 = 4;
$scope.starRating2 = 5;
$scope.starRating3 = 2;
$scope.hoverRating1 = $scope.hoverRating2 = $scope.hoverRating3 = 0;
$scope.click1 = function (param) {
console.log('Click(' + param + ')');
};
$scope.mouseHover1 = function (param) {
console.log('mouseHover(' + param + ')');
$scope.hoverRating1 = param;
};
$scope.mouseLeave1 = function (param) {
console.log('mouseLeave(' + param + ')');
$scope.hoverRating1 = param + '*';
};
}]);
app.directive('starRating', function () {
return {
scope: {
rating: '=',
maxRating: '@',
readOnly: '@',
click: "&",
mouseHover: "&",
mouseLeave: "&"
},
restrict: 'EA',
template:
"<div style='display: inline-block; margin: 0px; padding: 0px; cursor:pointer;' ng-repeat='idx in maxRatings track by $index'> \
<i class='fa fa-star-o' aria-hidden='true' ng-Click='isolatedClick($index + 1)' \
ng-mouseenter='isolatedMouseHover($index + 1)' \
ng-mouseleave='isolatedMouseLeave($index + 1)'></i> \
</div>",
compile: function (element, attrs) {
if (!attrs.maxRating || (Number(attrs.maxRating) <= 0)) {
attrs.maxRating = '5';
};
},
controller: function ($scope, $element, $attrs) {
$scope.maxRatings = [];
for (var i = 1; i <= $scope.maxRating; i++) {
$scope.maxRatings.push({});
};
$scope._rating = $scope.rating;
$scope.isolatedClick = function (param) {
if ($scope.readOnly == 'true') return;
$scope.rating = $scope._rating = param;
$scope.hoverValue = 0;
$scope.click({
param: param
});
};
$scope.isolatedMouseHover = function (param) {
if ($scope.readOnly == 'true') return;
$scope._rating = 0;
$scope.hoverValue = param;
$scope.mouseHover({
param: param
});
};
$scope.isolatedMouseLeave = function (param) {
if ($scope.readOnly == 'true') return;
$scope._rating = $scope.rating;
$scope.hoverValue = 0;
$scope.mouseLeave({
param: param
});
};
}
};
});
<!DOCTYPE html>
<html>
<head>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"/>
<link href="http://fontawesome.io/assets/font-awesome/css/font-awesome.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body>
<div class="container" ng-app="app" ng-controller="appController">
<div>
<div class="alert alert-success"> <span class="label label-info">Star Rating: Read/Write</span>
<div star-rating rating="starRating1" read-only="false" max-rating="10" click="click1(param)" mouse-hover="mouseHover1(param)" mouse-leave="mouseLeave1(param)"></div>
<div> <span class="label label-primary">Star Rating: {{starRating1}}</span>
<span class="label label-primary">Hover Rating: {{hoverRating1}}</span>
</div>
</div>
</div>
</div>
</body>
</html>
【问题讨论】:
-
我在示例中添加了 font-awesome star。但是,不知道如何获得悬停和点击状态。
-
任何建议或帮助将不胜感激。
标签: angularjs angularjs-directive font-awesome