Ash*_*hoo 9 javascript angularjs
我的应用程序在这个小提琴 我需要动态地从一个http服务渲染星级评分系统,其中当前的星星和最大的星星可以随每种情况而变化.
从$scope.current和
创建数组$scope.max - $scope.current并传递它们并ng-repeat在其上运行是一个好主意,或者有一个比这更优化的解决方案.
在AngularJs中迭代ng-repeat只重复X次
Nid*_*nan 28
星级评级可以静态(只读)或动态完成

如果您只想将评级显示为星级,请尝试以下方法
HTML
<body ng-app="starApp">
<div ng-controller="StarCtrl"> <span ng-repeat="rating in ratings">{{rating.current}} out of
{{rating.max}}
<div star-rating rating-value="rating.current" max="rating.max" ></div>
</span>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
脚本
var starApp = angular.module('starApp', []);
starApp.controller('StarCtrl', ['$scope', function ($scope) {
$scope.ratings = [{
current: 5,
max: 10
}, {
current: 3,
max: 5
}];
}]);
starApp.directive('starRating', function () {
return {
restrict: 'A',
template: '<ul class="rating">' +
'<li ng-repeat="star in stars" ng-class="star">' +
'\u2605' +
'</li>' +
'</ul>',
scope: {
ratingValue: '=',
max: '='
},
link: function (scope, elem, attrs) {
scope.stars = [];
for (var i = 0; i < scope.max; i++) {
scope.stars.push({
filled: i < scope.ratingValue
});
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
如果你想动态进行星级评分,请试试这个
HTML
<body ng-app="starApp">
<div ng-controller="StarCtrl"> <span ng-repeat="rating in ratings">{{rating.current}} out of
{{rating.max}}
<div star-rating rating-value="rating.current" max="rating.max" on-rating-selected="getSelectedRating(rating)"></div>
</span>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
脚本
var starApp = angular.module('starApp', []);
starApp.controller('StarCtrl', ['$scope', function ($scope) {
$scope.rating = 0;
$scope.ratings = [{
current: 5,
max: 10
}, {
current: 3,
max: 5
}];
$scope.getSelectedRating = function (rating) {
console.log(rating);
}
}]);
starApp.directive('starRating', function () {
return {
restrict: 'A',
template: '<ul class="rating">' +
'<li ng-repeat="star in stars" ng-class="star" ng-click="toggle($index)">' +
'\u2605' +
'</li>' +
'</ul>',
scope: {
ratingValue: '=',
max: '=',
onRatingSelected: '&'
},
link: function (scope, elem, attrs) {
var updateStars = function () {
scope.stars = [];
for (var i = 0; i < scope.max; i++) {
scope.stars.push({
filled: i < scope.ratingValue
});
}
};
scope.toggle = function (index) {
scope.ratingValue = index + 1;
scope.onRatingSelected({
rating: index + 1
});
};
scope.$watch('ratingValue', function (oldVal, newVal) {
if (newVal) {
updateStars();
}
});
}
}
});
Run Code Online (Sandbox Code Playgroud)
有一个美妙的教程在这里为更多的解释角度星级
小智 9
你甚至可以尝试使用angular-ui.这是链接.
只需添加此标记即可.
<rating ng-model="rate" max="max"
readonly="isReadonly"
on-hover="hoveringOver(value)"
on-leave="overStar = null">
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31947 次 |
| 最近记录: |