Adding a Select Box and its options dynamically to the DOM with AngularJS: A complete guide

After dynamically adding a Select Box with empty options to the DOM, what is the best way to populate it with options (ng-options)? I am facing a situation where users can add multiple select boxes to the DOM during runtime and then input values into these dynamically added select boxes.

I would greatly appreciate any assistance regarding this matter. Thank you very much.

Answer №1

Utilizing the ng-options attribute within a select element can assist in dynamically generating options. By keeping track of your dynamic options in the controller, the select element's options will automatically refresh when the data model is updated.

See It In Action

<div ng-controller="myCtrl">
  <select ng-model="selected" ng-options="o.label for o in options">
    <option value="">-------</option>  <!--not selected option-->
  </select>
  <button ng-click="addOption('option'+(options.length+1))">Add</button>
</div>

Controlling the Functionality

angular.module('app',[])
.controller("myCtrl",function($scope){
  $scope.options = [{label:"option1",value:"1"},{label:"option2",value:"2"},{label:"option3",value:"3"}];

  $scope.addOption = function(text){
    $scope.options.push({label:text,value:text});
  }
});

Take a look at this demo on jsFiddle.

We hope this solution proves to be beneficial for your needs.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Deleting an element in a Thinkster MEAN stack tutorial

After completing the Thinkster Mean Stack tutorial successfully, I proceeded to work on my own project. Everything was going well until I encountered a problem - deleting posts. The tutorial did not cover this aspect, and now I am stuck trying to figure ou ...

Adding data to a JSON object using AngularJS

When attempting to insert an object into a JSON object, I am encountering an issue where it duplicates the JSON object. Here is a breakdown of the scenario: <input type="text" style="width: 40% !important;" placeholder="Nom" class="input-sm" ng-model= ...

why is my angular listing malfunctioning when I try to compare two fields?

<div ng-controller="SamsungServicesCtrl"> <ion-content> <li class="item item-checkbox" ng-repeat="item in items" > <img src="{{item.icon}}" style="float:left;height:30px;width:30px;padding-right:5px;" & ...

Save a file in base64 format using Angular's FileSaver for downloading

Currently, I am facing a challenge in downloading a base64 file using angular-file-saver. Previously, I was able to achieve this without angular-file-saver just by using the following HTML code: <a ng-href="data:{{document.mimeType}};base64,{{document ...

converting an entire HTML layout into a PDF using JavaScript

I am attempting to convert an HTML design to PDF using the following script: <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.3/jspdf.min.js"></script>. I have designed a table to my liking, here is how it looks in HTML: https://i. ...

Utilize celery as the foundation for your model structure

Unconventional Request A unique challenge has been presented to me: I am required to utilize celery for communication with the database layer. The application consists of Flask and MongoDB on the backend, while Angular is used on the frontend. The clien ...

switching from grunt's autoprefixer to grunt's postcss

Since grunt-autoprefixer has been deprecated, I am looking to transition to grunt-postcss. Being new to the world of grunt and its dependencies, I couldn't find clear instructions in the documentation for this migration process. To complete the migr ...

Tips for utilizing multiple filters in AngularJS's $filter function

Two filters need to be applied on the content of a controller. The first filter should make the text lowercase, and the second one is a custom filter. I attempted to use them as follows: $filter('lowercase','cardShortNameRegex')(curre ...

Bootstrap modal with autocomplete feature

How can I display the last entered data by the user in a bootstrap modal? I attempted to use the HTML autocomplete="on" attribute but it did not work, similar to what is shown in this fiddle. After the user submits, on the subsequent attempt, it should pr ...

Issues with the functionality of minimized AngularJS JavaScript files

I combined all JS files into one and minified them, but now none of the site features are working. There were too many separate JS files to include individually, so I decided to merge them together. Is there a better method to reduce the number of HTTP r ...

Display confirmation pop-up when clicking outside of the modal window in Bootstrap

I am currently utilizing AngularJS (1.5.9) and ui-bootstrap in my application. I am looking to display a confirmation popup if the user clicks outside of an already open modal. I have attempted using both controlled exit with $uibModalInstance.close() and ...

Optimal way to send simulated data to Angular and Ionic applications

Currently, I am developing a mobile app using Ionic and AngularJS as the primary framework. However, I have encountered a problem that I need to address. I prefer not to send an HTTP request from my app to the backend API (which is built on Ruby on Rails) ...

None of the angular directives are functioning properly in this code. The function attached to the submit button is not executing as expected

I've experimented with various Angular directives in this code, but none seem to be functioning properly. I'm wondering if a library file is missing or if there's some issue within the code itself, potentially related to the jQuery file. The ...

Utilize the synchronization feature of ES6 Promises in Jasmine with the then/catch method

I have an angular controller that needs to be tested. This controller utilizes a service to fetch data from a server, and the service returns ES6 Promises. function MyController($scope, MyService) { $scope.doSomething = function () { MyService.foo() ...

The submit form and cordova functions are failing to trigger

I am encountering an issue where I cannot successfully call the submitForm() function when attempting to trigger it via ng-submit. The function does not execute as expected. How can I troubleshoot and resolve this problem? (function () { 'use str ...

AngularJS - Use promise instead of returning a data object

I am currently working on a project using AngularJS. Within my service.js file, I am attempting to retrieve some values. However, instead of receiving the actual data, I am getting back a promise object with some $$variables along with the desired data. ...

Steps for converting a Calendar into a JSON format

I am working on developing a REST application like the one shown below: @XmlRootElement @Entity @Table(name = "tb_verarq") public class Verificacao implements Serializable{ private static final long serialVersionUID = 1L; @Id @GeneratedValue ...

Output of ngResource compilation

Is there a way to retrieve a single result from my array $scope.trailers? I am encountering an issue where accessing the first index using $scope.trailers[0] returns undefined. The API call is made using ngResource. function getTrailers(pageNo){ ...

"Embracing the power of Spring Boot with AngularJS

I'm attempting to create a Spring Boot application. I have already set up the project, but I'm encountering an issue with my controller code: @RestController public class IndexController { @RequestMapping(value="/home",method = RequestMetho ...

The call stack limit has been exceeded due to the combination of Node, Express, Angular, and Angular-route

Embarking on a new SPA journey, my tech stack includes: Back-end: NodeJS + Express Front-end: Angular + Angular-route. Twitter Bootstrap Underscore Having followed many tutorials with similar stacks, my project files are structured as follows: pac ...