Skip to main content

How to bind selection box with options in AngularJs?

In angularjs, we are using the ng-options directive to display the selection.

Hello everyone, I am going to share the code-sample for binding country selection and after selected to country bind the cities behalf  of the selected country. The example in detail as given below.

Live demo example, go to below link http://embed.plnkr.co/Ru5phHLr6TIeazOeObmU/preview
.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
//The Countries CitiesList JSON
[{
        "code": 1,
        "country": "India",
        "cities": [ "New Delhi", "New Ashoknagar"]
 }, {
        "code": 2,
        "country": "Nepal",
        "cities": [ "Lumbini",   "Lumbini"
        ]
}, {
        "code": 3,
        "country": "United Kingdom",
        "cities": ["London","Manchester"]
}]
//The Countries CitiesList JSON
[{
        "code": 1,
        "country": "India",
        "cities": [ "New Delhi", "New Ashoknagar"]
 }, {
        "code": 2,
        "country": "Nepal",
        "cities": [ "Lumbini",   "Lumbini"
        ]
}, {
        "code": 3,
        "country": "United Kingdom",
        "cities": ["London","Manchester"]
}]
01
02
03
04
05
06
07
08
09
10
//The AngularJs code sample
angular.module('SelectBoxWithOptions', [])
    .controller('Controller', ['$scope', '$http', function ($scope, $http){
         $http.get('CountriesCitiesList.json')
            .success(function (data, status, headers, config) {
              $scope.locations = data;
          });
        }
  ]);
//The AngularJs code sample

angular.module('SelectBoxWithOptions', [])
    .controller('Controller', ['$scope', '$http', function ($scope, $http){
         $http.get('CountriesCitiesList.json')
            .success(function (data, status, headers, config) {
              $scope.locations = data;
          });
        }
  ]);
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//The live example code as given below.
<!DOCTYPE html>
<html>
<head>
</head>
<script>
    angular.module('SelectBoxWithOptions', [])
      .controller('Controller', ['$scope', '$http',
        function ($scope, $http) {
            $http.get('CountriesCitiesList.json')
              .success(function (data, status, headers, config) {
                  $scope.locations = data;
              });
        }
      ]);
</script>
<body ng-app="SelectBoxWithOptions">
    <h1>Select box with options</h1>
    <div ng-controller="Controller">
        <div>Countries</div>
        <select ng-model="countries" ng-options="loc.country for loc in locations">
            <option value="">Please chose country</option>
        </select>
        <div>Cities</div>
        <select ng-model='city' ng-options='c for c in countries.cities'>
            <option value="">Please chose city</option>
        </select>
    </div>
</body>
</html>
//The live example code as given below.
<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.js"></script>
</head>
<script>
    angular.module('SelectBoxWithOptions', [])
      .controller('Controller', ['$scope', '$http',
        function ($scope, $http) {
            $http.get('CountriesCitiesList.json')
              .success(function (data, status, headers, config) {
                  $scope.locations = data;
              });
        }
      ]);
</script>
<body ng-app="SelectBoxWithOptions">
    <h1>Select box with options</h1>
    <div ng-controller="Controller">
        <div>Countries</div>
        <select ng-model="countries" ng-options="loc.country for loc in locations">
            <option value="">Please chose country</option>
        </select>
        <div>Cities</div>
        <select ng-model='city' ng-options='c for c in countries.cities'>
            <option value="">Please chose city</option>
        </select>
    </div>
</body>
</html>

The output : go to http://embed.plnkr.co/Ru5phHLr6TIeazOeObmU/preview

Thank you!