AngularJS简单点击计数器无效

时间:2016-08-31 04:32:15

标签: angularjs counter increment buttongroup

我正在尝试构建计数器功能。我正在使用AngularJS和Bootstrap按钮组,我不确定冲突在哪里。以下是我的代码。有人可以帮忙吗?

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<body>
<div ng-app="meetingDetail" ng-controller="meetingDetailCtrl">

<div class="col-md-12">

    <div class="btn-group" data-toggle="buttons">
  <label class="btn btn-success btn-sm">
    <input type="radio" name="radioSecondBy" ng-click = "yesCount = yesCount+1" id="vYes" autocomplete="off">Yes
  </label>
  <label class="btn btn-warning btn-sm">
    <input type="radio" name="radioSecondBy" ng-click = "yesCount = yesCount+1" id="vNo" autocomplete="off"> No
  </label>
  <label class="btn btn-info btn-sm">
    <input type="radio" name="radioSecondBy" ng-click = "yesCount = yesCount+1" id="vAbstain" autocomplete="off"> Abstain
  </label>
</div>

{{yesCount}}

</div>
<script>
var app = angular.module('meetingDetail', []);
app.controller('meetingDetailCtrl', function($scope) {
    $scope.yesCount= 0;
});
</script>

</body>
</html>

2 个答案:

答案 0 :(得分:2)

单选按钮没有点击事件。

将click事件放在标签上而不是单选按钮

喜欢这个

  <label ng-click = "yesCount = yesCount+1" class="btn btn-info btn-sm">
    <input type="radio" name="radioSecondBy"  id="vAbstain" autocomplete="off"> Abstain
  </label>

<!DOCTYPE html>
<html>
  <head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    </head>
<body>
<div ng-app="meetingDetail" ng-controller="meetingDetailCtrl">

<div class="col-md-12">

    <div class="btn-group" data-toggle="buttons">
  <label class="btn btn-success btn-sm" ng-click = "yesCount = yesCount+1">
    <input type="radio" name="radioSecondBy"  id="vYes" autocomplete="off">Yes
  </label>
  <label class="btn btn-warning btn-sm" ng-click = "yesCount = yesCount+1" >
    <input type="radio" name="radioSecondBy" id="vNo" autocomplete="off"> No
  </label>
  <label ng-click = "yesCount = yesCount+1" class="btn btn-info btn-sm">
    <input type="radio" name="radioSecondBy"  id="vAbstain" autocomplete="off"> Abstain
  </label>
</div>
{{yesCount}}


</div>
</div>
  <script>
var app = angular.module('meetingDetail', []);
app.controller('meetingDetailCtrl', function($scope) {
    $scope.yesCount= 0;

});
</script>

</body>
</html>

答案 1 :(得分:0)

ng-click对单选按钮不起作用。您可以使用ng-change或观察变量

1)ng-change

<input type="radio" name="radioSecondBy" ng-change= "yesCount = yesCount+1" id="vYes" autocomplete="off">Yes

2)$ watch

$scope.$watch('yesCount', function(value) {
       $scope.yesCount = $scope.yesCount + 1;
 });

here是类似的问题