JavaScript - Simple Insert Form Value Using AngularJS

Submitted by: 
Language: 
Visitors have accessed this post 194 times.

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.