angularjs实现星星评分

自定义指令

app.directive('myStars', function () {
return {
require : '?ngModel', // ?ngModel
restrict : 'E',
replace : true,
templateUrl : 'ui/templateUrl/myStars.html',
scope: {ngModel : '='},
link: function ($scope, element, attrs, ngModel) {
$scope.myStars = [1,2,3,4,5];
$scope.clickCnt = 1;
$scope.$watch('ngModel', function(newValue) {
var dataList = newValue;
console.log(dataList);
if(!dataList) return;
$scope.myStar = dataList;
$scope.clickCnt = dataList;
})
$scope.stars = function (myStar) {
$scope.clickCnt = myStar;
ngModel.$setViewValue(myStar);
} $scope.mouseoverStar = function (myStar) {
$scope.hoverCnt = myStar;
}
$scope.mouseleaveStar = function (myStar) {
$scope.hoverCnt = 1;
}
}
}
});

里面的myStars.html

<div>
<meta charset="utf-8">
<ul class="my-stars-list">
<li class="my-stars-items" ng-mouseover="mouseoverStar(myStar)" ng-mouseleave="mouseleaveStar(maStar)" ng-click="stars(myStar)" ng-class="{myStarsSel: clickCnt > $index , myStarHover: hoverCnt > $index }" ng-repeat="myStar in myStars track by $index" title="{{ myStar }}星">{{ myStar }}</li>
</ul>
</div>
html中的使用如下:
<my-stars ng-model="drLevel"></my-stars>

css样式:

.my-stars-list{
height: 34px;
line-height: 34px;
display: -webkit-box;
display: -webkit-flex;
display: flex;
}
.my-stars-items{
cursor:pointer;
font-size:;
width: 34px;
line-height: 34px;
height:34px;
background-image: url(../images/star-off-big.png);
background-repeat: no-repeat;
background-position: center center;
}
.myStarHover.my-stars-items{
background-image: url(../images/star-on-big.png);
background-repeat: no-repeat;
background-position: center center;
}
.myStarsSel.my-stars-items{
background-image: url(../images/star-on-big.png);
background-repeat: no-repeat;
background-position: center center;
}

css中的图片:

完成的效果:

获取点击的是多少直接

var level = $scope.drLevel;

最新文章

  1. (转)SVN服务器搭建和使用(二)
  2. ehcache入门
  3. Linux system V
  4. IOS AsyncSocket
  5. SqlServer循环读取配置
  6. 轻量级GUI enlightenment
  7. FROM CSDN TO CNBLOGS
  8. python mysqlDB
  9. 华为上机题汇总----java
  10. HttpServletResponse对象(一)
  11. Spring初识(通过小实例清晰认识Spring)
  12. anaconda的fish shell支持
  13. MySQL递归查询_函数语法检查_GROUP_CONCAT组合结果集的使用
  14. netty(七) Handler的执行顺序
  15. Jenkins+Ansible+Gitlab自动化部署三剑客-Ansible本地搭建
  16. PHP调用接口用post方法传送json数据
  17. HDU6201
  18. C#数组之 []、List、Array、ArrayList应用
  19. 腾讯在线文档发布:实现QQ、微信多平台多人协作编辑
  20. php的循环与引用的一个坑

热门文章

  1. 回顾.NET Remoting分布式开发
  2. Powershell Get File/Disk Size
  3. 【Python之路】第十七篇--Ajax全套
  4. &lt;2014 08 29&gt; MATLAB的软件结构与模块、工具箱简示
  5. python pip源配置,pip配置文件存放位置
  6. js事件委托和jQuery事件绑定on , off , one , bind , unbind , die
  7. RT-Thread内核之线程调度(三)
  8. subprocess 模块 与终端相互交互
  9. mysql 如何找出两张表之间的关系
  10. 吴超老师课程--HBASE的Java_API