I have a code like this:
JavaScript
x
24
24
1
<!DOCTYPE html>
2
<html xmlns="http://www.w3.org/1999/xhtml">
3
<head>
4
<title>Welcome to LearnKode - A code learning platform</title>
5
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
6
</head>
7
<body ng-app="changeExample">
8
<div ng-controller="ExampleController">
9
<div class="container">
10
<div class="col-md-3 well">
11
Are you developer <input type="checkbox" ng-model="isTrue" ng-change="count=count+1" />
12
Count: {{count}}
13
<pre>{{isTrue}}</pre>
14
</div>
15
</div>
16
</div>
17
<script>
18
var app = angular.module("changeExample", []);
19
app.controller('ExampleController', ['$scope', function ($scope) {
20
$scope.isTrue = true;
21
}]);
22
</script>
23
</body>
24
</html>
In this code when check the checkbox the count will be incremented. Here how to i check if checkbox is ticked, then only incremented, otherwise if untick, it will decremented. Please anyone help.
Advertisement
Answer
JavaScript
1
29
29
1
<!DOCTYPE html>
2
<html xmlns="http://www.w3.org/1999/xhtml">
3
4
<head>
5
<title>Welcome to LearnKode - A code learning platform</title>
6
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
7
</head>
8
9
<body ng-app="changeExample">
10
<div ng-controller="ExampleController">
11
<div class="container">
12
<div class="col-md-3 well">
13
Are you developer
14
<input type="checkbox" ng-model="isTrue" ng-change="isTrue ? (count=count+1) :(count=count-1) " />Count: {{count}}
15
<pre>{{isTrue}}</pre>
16
</div>
17
</div>
18
</div>
19
<script>
20
var app = angular.module("changeExample", []);
21
app.controller('ExampleController', ['$scope',
22
function($scope) {
23
$scope.isTrue = false;
24
}
25
]);
26
</script>
27
</body>
28
29
</html>
try changing ng-change .