Question

I have implemented the bootstrap star rating in angularjs...everything works fine but am unable to get final rating value after form submit ..below is code

 //enter code here js 
 angular.module('plunker', ['ui.bootstrap']);

var RatingDemoCtrl = function ($scope) {

     $scope.submit = function() {
         console.log($scope.overStar) ; //giving always null
     }

     $scope.rate = 1;
     $scope.max = 5;
     $scope.isReadonly = false;

      $scope.hoveringOver = function(value,object) {
        $scope.overStar = value;
        $scope.percent = 100 * (value / $scope.max);
      };
        };

enter code here html


 <!doctype html>
<html ng-app="plunker">
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js"></script>
    <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.6.0.js"></script>
    <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">

</head>
<body>
<div ng-controller="RatingDemoCtrl" class="well well-small">
<form class="Scroller-Container" ng-submit="submit()" >

    <rating value="rate" max="max" readonly="isReadonly" on-hover="hoveringOver(value)" on-leave="overStar = null" ></rating>
    <span class="badge" ng-class="{'badge-warning': percent<30, 'badge-info': percent>=30 && percent<70, 'badge-success': percent>=70}" ng-show="overStar && !isReadonly">{{percent}}%</span>

</div>
<input type="submit" id="submit" value="Submit" />
</form>
</body>
</html>

i tried ng-click to set value but ng-click not getting trigger dont know y ...anyone had done this before .... need help ...

Was it helpful?

Solution

Add hoveringLeave method.

Here is fixed code:

JS

 angular.module('plunker', ['ui.bootstrap']);

var RatingDemoCtrl = function ($scope) {

    $scope.myRate = 0;

     $scope.submit = function() {
         console.log($scope.percent) ; 
     }

     $scope.rate = 1;
     $scope.max = 5;
     $scope.isReadonly = false;
     $scope.percent = 20;

      $scope.hoveringOver = function(value,object) {
        console.log('hoveringOver', value);
        $scope.overStar = value;
        $scope.percent = (100 * $scope.overStar) / $scope.max;
      };

       $scope.hoveringLeave = function(rate) {
         console.log('hoveringLeave',  $scope.rate);

       $scope.percent = (100 * $scope.rate) / $scope.max;
      };
    };

HTML

<form class="Scroller-Container" ng-submit="submit()" >
    <rating  value="rate" max="max" readonly="isReadonly" on-hover="hoveringOver(value)" on-leave="hoveringLeave(rate)" ></rating>
    <span class="badge" ng-class="{'badge-warning': percent<30, 'badge-info': percent>=30 && percent<70, 'badge-success': percent>=70}" ng-show="overStar && !isReadonly">{{percent}}%</span>

<input type="submit" id="submit" value="Submit" />
</form>

Demo Plunker

OTHER TIPS

The reason when you submit $scope.overStar always return null is in your html you have 'on-leave="overStar = null"', so every time your mouse leaves that variable $scope.overStar will be set to null.

The above solution is also not correct, you assume when mouse leave it is time that user pick a rating, mouse leave is not equal click. If you set it that way, you will have more ratings than you really need.

I am using Angular 1.2.* with UI Bootstrap 0.10.0 and ng-click works fab for me:

HTML:

<rating value="rate" max="5" ng-click="save(rate)"></rating>

Ctrl:

$scope.save = function (score) { console.log('Rating: ', score); };

I would suggest instead of adding another button,you can do a $watch over rate in the scope and check if the value has changed.

$scope.$watch('rate', function(newValue, oldValue) {
    if(newValue!==oldValue){
        alert(newValue+' '+oldValue);
    }
});

if that case occurs you can persist new change.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top