Saturday, March 21, 2015

ng-change directive with checkbox

Change events are used to handle user interactions with controls like TextBox, Dropdown list, checkboxes etc. In this post we shall see on how to handle change events associated with the checkbox control.

Change event in these controls can be handled by creating a handler function and associating the event to the function using the ng-change directive. The following is the syntax for the ng-change directive and the handler function.

ng-change="HandleChange()
In the following example the checkbox control has a ng-change directive which binds the events from the checkbox controller to the function HandleChange(), when the user checkes/un-checks the checkbox control the HandleChange() function is called and the currently selection status is displayed in the label.
<html ng-app="changeCheckboxApp">
<head>
    <meta charset="utf-8">
    <title>AngularJS - Basic</title>
    <script src="angular.min.js"></script>
    <script>
var app = angular.module('changeCheckboxApp', []);

function changeCheckboxController($scope) {
          $scope.checkedStatus = "false";
          //
          $scope.HandleChange = function() {
             $scope.checkedStatus = $scope.checkedFlag
          }
};
</script>
</head>
<body>
          <div ng-controller="changeCheckboxController">
                   <input type="checkbox" ng-model="checkedFlag" ng-change="HandleChange()"> <br/><br/>
                             Checked : {{checkedStatus}}
                   </div>
          </div>
</body>
</html>




Search Flipkart Products:
Flipkart.com

No comments: