JavaScript - Simple Insert Form Value Using AngularJS

In this tutorial we will create a Simple Insert Form Value Using AngularJS. This code will store a temporary data in the table without using databases when the user fill up the required field. The code itself use a angular directives that can insert form inputs as an array to be able to store a temporary data without sending to the database server. This a user-friendly program feel free to modify and use it to your system. We will be using AngularJS as a comprehensive JavaScript framework that extend the browser capabilities. It handles the heavy lifting of DOM manipulation and AJAX request in a whole.

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="containet-fluid">
  10. <a class="navbar-brand" href="https://www.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- Simple Insert Form Value Using AngularJS</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add student</button>
  18. <div class="container-fluid">
  19. <br />
  20. <br />
  21. <table class="table table-bordered">
  22. <thead class="alert-info">
  23. <tr>
  24. <th>Student ID</th>
  25. <th>Firstname</th>
  26. <th>Lastname</th>
  27. <th>Address</th>
  28. </tr>
  29. </thead>
  30. <tr ng-repeat="student in students">
  31. <td>{{$index+1}}</td>
  32. <td>{{student.firstname}}</td>
  33. <td>{{student.lastname}}</td>
  34. <td>{{student.address}}</td>
  35. </tr>
  36. </tbody>
  37. </table>
  38. </div>
  39. </div>
  40. <div class="modal fade" id="form_modal" aria-hidden="true">
  41. <div class="modal-dialog">
  42. <div class="modal-content">
  43. <form>
  44. <div class="modal-header">
  45. <h3 class="modal-title">Add Student</h3>
  46. </div>
  47. <div class="modal-body">
  48. <div class="col-md-2"></div>
  49. <div class="col-md-8">
  50. <div class="form-group">
  51. <label>Firstname</label>
  52. <input type="text" class="form-control" ng-model="newStudent.firstname"/>
  53. </div>
  54. <div class="form-group">
  55. <label>Lastname</label>
  56. <input type="text" class="form-control" ng-model="newStudent.lastname"/>
  57. </div>
  58. <div class="form-group">
  59. <label>Address</label>
  60. <input type="text" class="form-control" ng-model="newStudent.address"/>
  61. </div>
  62. </div>
  63. </div>
  64. <br style="clear:both;"/>
  65. <div class="modal-footer">
  66. <button class="btn btn-danger" data-dismiss="modal"><span class = "glyphicon glyphicon-remove"></span> Remove</button>
  67. <button class="btn btn-primary" ng-click="saveStudent()" data-dismiss="modal"><span class = "glyphicon glyphicon-save"></span> Save</button>
  68. </div>
  69. </form>
  70. </div>
  71. </div>
  72. </div>
  73. </body>
  74. <script src = "js/angular.js"></script>
  75. <script src = "js/script.js"></script>
  76. <script src = "js/jquery-3.2.1.min.js"></script>
  77. <script src = "js/bootstrap.js"></script>
  78. </html>

Creating the Main Function

This code contains the main function of the application. This code will store a temporary data 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.
  1. var app = angular.module("myModule", [])
  2. .controller("myController", function($scope){
  3.  
  4. $scope.newStudent = {};
  5.  
  6. $scope.students = [
  7. {firstname: "John", lastname: "Smith", address: "New York"},
  8. {firstname: "Claire", lastname: "Temple", address: "Racoon City"}
  9. ];
  10.  
  11. $scope.saveStudent = function(){
  12. $scope.students.push($scope.newStudent);
  13. $scope.newStudent = {};
  14. };
  15.  
  16. });
There you have it we successfully created a Simple Insert Form Value 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