In this article I will explain how to check whether a CheckBox is checked or unchecked using AngularJS.
The status of the CheckBox i.e. checked or unchecked will be determined on Button click inside the AngularJS Controller.
AngularJS: Check if a CheckBox is checked or unchecked
The below HTML Markup consists of an HTML DIV to which ng-app and ng-controller AngularJS directives have been assigned.
The AngularJS app HTML DIV consists of an HTML CheckBox set with ng-model directive and a Button set with ng-click directive.
When the Button is clicked, the CheckPassport function of the Controller gets called, which first checks the value of the model variable HasPassport and based on whether it is true or false it displays respective messages using JavaScript Alert message Box.
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', [])
app.controller('MyController', function ($scope, $window) {
$scope.CheckPassport = function () {
if ($scope.HasPassport) {
$window.alert("CheckBox is checked.");
} else {
$window.alert("CheckBox is not checked.");
}
};
});
</script>
<div ng-app="MyApp" ng-controller="MyController">
Do you have Passport?
<label for="chkPassport">
<input id="chkPassport" type="checkbox" ng-model="HasPassport" />
</label>
<input type="button" value = "Check Passport" ng-click = "CheckPassport()" />
</div>
</body>
</html>
Screenshot
Demo
Downloads