Kiran Kolli Kiran Kolli - 3 months ago 8
AngularJS Question

AngularJS Simple click counter not working

I'm trying to build a counter functionality. I'm using AngularJS with Bootstrap button group and I'm not sure where the conflict is. Below is my code. Can someone please help?

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

Answer

Radio buttons don't have click event.

Put click event on label instead of radio button

Like this

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