How to bind selection box with options in the angular js?

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!
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.