JavaScript - Insert Select Option Dynamically Using AngularJS

In this tutorial we will create a Insert Select Option Dynamically using AngularJS. This code will create a insert an additional option value when user submit the form. The code itself use AngularJS directives ng-click() to initiate a method that insert a new possible select value dynamically providing the information needed. This a user-friendly program feel free to modify and use it to your system. We will be using AngularJS as a framework which has additional custom HTML attributes embedded into it. It can interprets those attributes as directives to bind inputted parts of the page to a model that represent as a standard JavaScript variables.

Getting Started:

First you have to download Bootstrap, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/. And, this is the link for the AngularJS https://angularjs.org/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it index.html.
  1. <!DOCTYPE html>
  2. <html lang="en" ng-app="myModule">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6.         </head>
  7. <body ng-controller="myController">
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">JavaScript - Insert Select Option Dynamically Using AngularJS</h3>
  16.                 <hr style="borde-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-3"></div>
  18.                 <div class="col-md-6">
  19.                         <form>
  20.                                 <div class="form-group">
  21.                                         <label>Enter a word</label>
  22.                                         <input type="text" ng-model="newName.name" class="form-control"/>
  23.                                         <br />
  24.                                         <center><button type="button" ng-click="saveName()" class="btn btn-primary">Submit</button></center>
  25.                                 </div>
  26.                         </form>
  27.                         <br />
  28.                         <center>
  29.                                 <div class="form-group">
  30.                                         <select class="form-control" ng-model="selectedName" ng-options="item.name for item in names">
  31.                                         </select>
  32.                                 </div>
  33.                         </center>
  34.                 </div>
  35.         </div>
  36. <script src="js/angular.js"></script>  
  37. <script src="js/script.js"></script>   
  38. </body>
  39. </html>

Creating the Main Function

This code contains the main function of the application. This code will insert select value when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory.
  1. var app = angular.module("myModule", [])
  2.                                                            .controller("myController", function($scope){
  3.  
  4.                                                                 $scope.newName = {};
  5.  
  6.                                                                 $scope.names = [
  7.                                                                         {name: "Jason Smith"},
  8.                                                                         {name: "Claire Temple"},
  9.                                                                 ];
  10.  
  11.                                                                 $scope.saveName = function(){
  12.                                                                         if($scope.name ==""){
  13.                                                                                 alert("Please enter something first!");
  14.                                                                         }else{
  15.                                                                                 $scope.names.push($scope.newName);
  16.                                                                                 $scope.newName = {};
  17.                                                                         }
  18.                                                                 };             
  19.  
  20.                                                                
  21. });
There you have it we successfully created a Insert Select Option Dynamically using AngularJS. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Add new comment