1

我已经在 angularjs 中实现了引导星级评分...一切正常,但在表单提交后无法获得最终评分值..下面是代码

 //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>

我尝试使用 ng-click 设置值,但 ng-click 没有触发不知道你……之前有人做过……需要帮助……

4

4 回答 4

2

添加hoveringLeave 方法。

这是固定代码:

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>

演示Plunker

于 2013-10-29T08:18:51.057 回答
1

当你提交 $scope.overStar 总是返回 null 的原因是在你的 html 中你有'on-leave="overStar = null"',所以每次你的鼠标离开变量 $scope.overStar 都会被设置为 null。

上述解决方案也不正确,您假设鼠标离开时是用户选择评分的时候,鼠标离开不等于点击。如果你这样设置,你将获得比你真正需要的更多的评级。

于 2014-01-04T15:32:13.293 回答
0

我正在使用带有 UI Bootstrap 0.10.0 的 Angular 1.2.* 并且 ng-click 对我来说非常有用:

HTML:

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

控制:

$scope.save = function (score) { console.log('Rating: ', score); };
于 2014-01-28T14:34:49.433 回答
0

我建议您不要添加另一个按钮,而是可以在范围内执行 $watch over rate 并检查值是否已更改。

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

如果发生这种情况,您可以坚持新的更改。

于 2014-09-16T17:57:26.907 回答