Tuesday, March 17, 2015

ng-change directive with Textbox

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 textbox 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 textbox control has as ng-change directive which binds the events from the textbox controller to the function HandleChange(), the letters entered by the user are immediately handled by the HandleChange() function and the letters are displayed in the label.

<html ng-app="changeTextboxApp">
<head>
    <meta charset="utf-8">
    <title>AngularJS - Basic</title>
    <script src="angular.min.js"></script>
    <script>
var app = angular.module('changeTextboxApp', []);

function changeTextboxController($scope) {
          $scope.HandleChange = function() {
             $scope.YourName = $scope.myName
          }
};
</script>
</head>
<body>
          <div ng-controller="changeTextboxController">
                   <input type="text" ng-model="myName" ng-change="HandleChange()"> <br/><br/>
                             Your Name is : {{YourName}}
                   </div>
          </div>
</body>
</html>

Output:


Search Flipkart Products:
Flipkart.com

No comments: