File tree Expand file tree Collapse file tree 3 files changed +61
-13
lines changed
Expand file tree Collapse file tree 3 files changed +61
-13
lines changed Original file line number Diff line number Diff line change 11( function ( ) {
2- var app = angular . module ( "rateApp" , [ ] ) ;
3-
2+ var app = angular . module ( "rateApp" , [ ] ) ;
3+ app . controller ( 'ratingCrtl' , [ '$scope' , '$window' ,
4+ function ( $scope , $window ) {
5+ $scope . rating = 5 ;
6+ $scope . onRating = function ( rating ) {
7+ console . log ( rating ) ;
8+ } ;
9+ }
10+ ] ) ;
411 app . directive ( 'rating' , function ( ) {
512 return {
613 restrict : 'A' ,
7- templateUrl :'rate-template.html' ,
8- scope :{
9- max :'='
14+ templateUrl : 'rate-template.html' ,
15+ scope : {
16+ max : '=' ,
17+ readonly : '@' ,
18+ ratingValue : '=' ,
19+ onRating : '&' ,
1020 } ,
1121 link : function ( scope , elem , attrs ) {
12- scope . stars = [ ] ;
13- for ( var i = 0 ; i < scope . max ; i ++ ) {
14- scope . stars . push ( { } ) ;
15- }
22+
23+ var updateStars = function ( ) {
24+ scope . stars = [ ] ;
25+ for ( var i = 0 ; i < scope . max ; i ++ ) {
26+ scope . stars . push ( { filled : i < scope . ratingValue } ) ;
27+ }
28+ } ;
29+
30+ scope . toggle = function ( index ) {
31+ if ( scope . readonly && scope . readonly === 'true' ) {
32+ return ;
33+ }
34+ scope . ratingValue = index + 1 ;
35+ scope . onRating ( ) ;
36+ } ;
37+
38+ scope . $watch ( 'ratingValue' , function ( oldVal , newVal ) {
39+ if ( newVal ) {
40+ updateStars ( ) ;
41+ }
42+ } ) ;
1643 }
1744 } ;
1845 } ) ;
Original file line number Diff line number Diff line change 11< ul class ="rating ">
2- < li ng-repeat ="star in stars " class ="filled ">
2+ < li ng-repeat ="star in stars " ng- class ="star " ng-click =" toggle($index) ">
33 ★
44 </ li >
55</ ul >
Original file line number Diff line number Diff line change 44 < title > Test</ title >
55 < meta charset ="UTF-8 ">
66 < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
7-
7+ < style >
8+ .rating {
9+ color : # a9a9a9 ;
10+ margin : 0 ;
11+ padding : 0 ;
12+ }
13+ ul .rating {
14+ display : inline-block;
15+ }
16+ .rating li {
17+ list-style-type : none;
18+ display : inline-block;
19+ padding : 1px ;
20+ text-align : center;
21+ font-weight : bold;
22+ cursor : pointer;
23+ }
24+ .rating .filled {
25+ color : # 21568b ;
26+ }
27+ </ style >
828 </ head >
929 < body >
10- < div ng-app ="rateApp ">
11- < div rating max ="10 "> </ div >
30+ < div ng-app ="rateApp " ng-controller ="ratingCrtl ">
31+ Current Rating : {{rating}} < br >
32+ < div rating rating-value ="rating " max ="10 " on-rating ="onRating(rating) "> </ div >
1233 </ div >
1334 <!-- Scripts -->
1435 < script src ="js/plugin/jquery.min.js "> </ script >
You can’t perform that action at this time.
0 commit comments