Saturday, March 21, 2015

ng-change directive with Dropdown list

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


In the following example the dropdown list control has as ng-change directive which binds the events from the dropdown list controller to the function HandleChange(), when the user changes the selection in the dropdown list control the HandleChange() function is called and the currently selected language is displayed in the label.

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

function changeDropdownController($scope) {
    $scope.myLanguages = [
                                      "code": "en",
                                      "name": "English"
                                      "code": "de",
                                      "name": "German"
          $scope.HandleChange = function() {
             $scope.SelectedLanguage = $scope.myLanguage
          <div ng-controller="changeDropdownController">
                             ng-options=" as for value in myLanguages"
                             Selected Language: {{SelectedLanguage}}

Search Flipkart Products:

No comments: