JavaScript - Add New Data Using AngularJS

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

In this tutorial we will create a Add New Data Using AngularJS. This code will store a temporary data in the HTML table without databases when the user submit the form input. The code use angular directives that can add new data as an array in order 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 - Add New Data Using AngularJS</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-4">
  19. <form>
  20. <div class="form-group">
  21. <label>Firstname</label>
  22. <input type="text" class="form-control" ng-model="newUser.firstname"/>
  23. </div>
  24. <div class="form-group">
  25. <label>Lastname</label>
  26. <input type="text" class="form-control" ng-model="newUser.lastname"/>
  27. </div>
  28. <div class="form-group">
  29. <label>Address</label>
  30. <input type="text" class="form-control" ng-model="newUser.address"/>
  31. </div>
  32. <center><button class="btn btn-primary" ng-click="saveUser()">Add</button></center>
  33. </form>
  34. </div>
  35. <div class="col-md-8">
  36. <table class="table table-bordered">
  37. <thead class="alert-info">
  38. <tr>
  39. <th>User ID</th>
  40. <th>Firstname</th>
  41. <th>Lastname</th>
  42. <th>Address</th>
  43. </tr>
  44. </thead>
  45. <tr ng-repeat="user in users">
  46. <td>{{$index+1}}</td>
  47. <td>{{user.firstname}}</td>
  48. <td>{{user.lastname}}</td>
  49. <td>{{user.address}}</td>
  50. </tr>
  51. </tbody>
  52. </table>
  53. </div>
  54. </div>
  55. </body>
  56. <script src="js/angular.js"></script>
  57. <script src="js/script.js"></script>
  58. </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.newUser = {};
  5.  
  6. $scope.users = [
  7. {firstname: "John", lastname: "Smith", address: "New York"}
  8. ];
  9.  
  10. $scope.saveUser = function(){
  11. $scope.users.push($scope.newUser);
  12. $scope.newUser = {};
  13. };
  14.  
  15. });

There you have it we successfully created a Add New Data 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.