How to Add Dynamic Options for Multiple Selects Inside ng-repeat Directive ? Last Updated : 29 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Given an HTML document containing some options element and the task is to add an array of javascript objects dynamically with multiple selects using ng-repeat in angularJS. Approach: The task is done using ng-repeat which loops through an array. Let's call this array "models". Each select menu present in the DOM is modeled to its specific index in the array. For example, the 2nd select menu would be modeled to the 2nd object in the model's objects array. To add more select menus to the DOM, we only need to push an empty object to the model's array, the ng-repeat directive takes care of the rest of reproduction. Example 1: In this example, we will add multiple selects and display the selected data. html <!DOCTYPE html> <html ng-app="gfg"> <head> <meta charset="utf-8" /> <script data-require="[email protected]" src= "https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"> </script> <script> var app = angular.module('gfg', []); app.controller('MainCtrl', function($scope) { $scope.models = [{}]; $scope.countries = ['India', 'Japan', 'US']; $scope.states = { India: ['UP', 'MP', 'Bihar'], Japan: ['Tokyo', 'Yokohama'], US: ['California', 'Texas'], } $scope.addRow = function() { $scope.models.push({}); } $scope.getState = function(country) { return $scope.states[country]; } }); </script> </head> <body ng-controller="MainCtrl"> <center> <h1 style="color: green;"> GeeksForGeeks </h1> <table> <tr> <th>Country</th> <th>State</th> <th>Action</th> </tr> <tr ng-repeat="model in models"> <td> <select ng-options= "country as country for country in countries" ng-model="model.country" ng-change='getState(model.country)'> </select> </td> <td> <select ng-options= "state as state for state in getState(model.country)" ng-model="model.state"> </select> </td> <td> <button ng-click="addRow()">Add Row</button> </td> </tr> </table> <h3 style="color:green">Selections</h3> <p ng-repeat="model in models"> {{model.country}} - {{model.state}} </p> </center> </body> </html> Output: All the data is successfully added to the objects array. Example 2: In this example, we prepopulate the models array. html <!DOCTYPE html> <html ng-app="gfg"> <head> <meta charset="utf-8" /> <script data-require="[email protected]" src= "https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"> </script> <script> var app = angular.module('gfg', []); // Prepopulate the models array here app.controller('MainCtrl', function($scope) { $scope.models = [{ country: 'India', state: 'UP' }]; $scope.countries = ['India', 'Japan', 'US']; $scope.states = { India: ['UP', 'MP', 'Bihar'], Japan: ['Tokyo', 'Yokohama'], US: ['California', 'Texas'], } $scope.addRow = function() { $scope.models.push({}); } $scope.getState = function(country) { return $scope.states[country]; } }); </script> </head> <body ng-controller="MainCtrl"> <center> <h1 style="color: green;"> GeeksForGeeks </h1> <table> <tr> <th>Country</th> <th>State</th> <th>Action</th> </tr> <tr ng-repeat="model in models"> <td> <select ng-options= "country as country for country in countries" ng-model="model.country" ng-change='getState(model.country)'> </select> </td> <td> <select ng-options= "state as state for state in getState(model.country)" ng-model="model.state"> </select> </td> <td> <button ng-click="addRow()">Add Row</button> </td> </tr> </table> <h3 style="color:green">Selections</h3> <p ng-repeat="model in models"> {{model.country}} - {{model.state}} </p> </center> </body> </html> Output: We see that the page now always contain country "India" and state "UP" as its prepopulated on page load. Comment More info T thvardhan Follow Improve Article Tags : Web Technologies AngularJS AngularJS-Misc Explore AngularJS BasicsAngularJS Tutorial 5 min read Introduction to AngularJS 4 min read Angular CLI | Angular Project Setup 3 min read AngularJS Expressions 2 min read AngularJS Modules 3 min read AngularJS ng-model Directive 4 min read AngularJS Data Binding 4 min read AngularJS Controllers 3 min read AngularJS | Scope 2 min read AngularJS Services 4 min read AngularJS | AJAX - $http 3 min read AngularJS | Tables 2 min read AngularJS Select Boxes 2 min read AngularJS SQL 3 min read AngularJS HTML DOM 2 min read AngularJS Events 3 min read AngularJS | Forms 3 min read AngularJS Form Validation 3 min read AngularJS | API 2 min read AngularJS and W3.CSS 2 min read AngularJS Includes 3 min read AngularJS Animations 1 min read AngularJS | Application 3 min read AngularJS DirectivesAngularJS Directives 9 min read AngularJS ng-app Directive 1 min read AngularJS ng-bind Directive 2 min read AngularJS ng-bind-html Directive 2 min read AngularJS ng-bind-template Directive 2 min read AngularJS ng-blur Directive 1 min read AngularJS ng-change Directive 2 min read AngularJS ng-checked Directive 2 min read AngularJS ng-class Directive 2 min read AngularJS ng-class-even Directive 2 min read AngularJS ng-class-odd Directive 2 min read AngularJS ng-click Directive 2 min read AngularJS ng-cloak Directive 2 min read AngularJS ng-controller Directive 2 min read AngularJS Directives Complete Reference 2 min read AngularJS FiltersAngularJS | Filters 7 min read AngularJS currency Filter 2 min read AngularJS | date Filter 2 min read AngularJS filter Filter 3 min read AngularJS json Filter 2 min read AngularJS limitTo Filter 2 min read AngularJS lowercase Filter 1 min read AngularJS number Filter 1 min read AngularJS orderBy Filter 4 min read AngularJs uppercase Filter 1 min read AngularJS Converting FunctionsAngularJS angular.lowercase() Function 2 min read AngularJS angular.uppercase() Function 1 min read AngularJS angular.forEach() Function 1 min read AngularJS Comparing FunctionsAngularJS angular.isArray() Function 2 min read AngularJS angular.isDate() Function 2 min read AngularJS angular.isDefined() Function 2 min read AngularJS angular.isElement() Function 2 min read AngularJS angular.isFunction() Function 2 min read AngularJS angular.isNumber() Function 2 min read AngularJS angular.isObject() Function 2 min read AngularJS | angular.isString() Function 1 min read AngularJS angular.isUndefined() Function 2 min read AngularJS angular.equals() Function 2 min read AngularJS angular.toJson() Function 2 min read AngularJS QuestionsHow to bundle an Angular app for production? 4 min read How to add many functions in one ng-click directive? 2 min read How to directly update a field by using ng-click in AngularJS ? 3 min read How to Add Dynamic Options for Multiple Selects Inside ng-repeat Directive ? 3 min read How to detect when an @Input() value changes in Angular? 3 min read How to open popup using Angular and Bootstrap ? 2 min read How to reload or re-render the entire page using AngularJS? 2 min read How to add input fields dynamically on button click in AngularJS ? 2 min read How to Create Button Dynamically with Click Event in Angular ? 2 min read How to use jQuery in Angular ? 2 min read AngularJS Examples 2 min read Like