python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
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

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions