我已经在angularjs中实现了bootstrap星级...一切正常但在表单提交后无法获得最终评级值..下面是代码
//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没有触发器不知道y ...之前有人做过这个......需要帮助......
答案 0 :(得分: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
答案 1 :(得分:1)
当你提交$ scope.overStar总是返回null的原因是在你的html中你有'on-leave =“overStar = null”',所以每次你的鼠标离开那个变量$ scope.overStar将被设置为null
以上解决方案也不正确,您假设当鼠标离开时是用户选择评级的时间,鼠标离开不等于点击。如果你这样设置,你将获得比你真正需要的更多的评级。
答案 2 :(得分:0)
我正在使用Angular 1.2。*和UI Bootstrap 0.10.0以及ng-click为我工作:
<rating value="rate" max="5" ng-click="save(rate)"></rating>
$scope.save = function (score) { console.log('Rating: ', score); };
答案 3 :(得分:0)
我建议您不要再添加其他按钮,而是可以在范围内执行$ watch over rate并检查值是否已更改。
$scope.$watch('rate', function(newValue, oldValue) {
if(newValue!==oldValue){
alert(newValue+' '+oldValue);
}
});
如果发生这种情况,您可以坚持新的更改。