JavaScript - Create, Read, Delete, Update Functions Using AngularJS

In this tutorial we will create a Create, Read, Delete, Update Functions Using AngularJS. This code can create, read, delete, update data the when user click one the buttons. The code use AngularJS directives to call a function that divided into a different functionalities that manipulate the data entry within the array object. 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="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 - Create, Read, Delete, Update Functions Using AngularJS</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-4">
  18. <form>
  19. <div class = "form-group">
  20. <label>Full Name</label>
  21. <input type = "text" class = "form-control" ng-model = "newMember.name"/>
  22. </div>
  23. <div class = "form-group">
  24. <label>Address</label>
  25. <input type = "text" class = "form-control" ng-model = "newMember.address"/>
  26. </div>
  27.  
  28. <center><button type="button" class="btn btn-primary" ng-click = "saveMember()"><span class = "glyphicon glyphicon-save"></span> Save</button></center>
  29. </form>
  30. </div>
  31. <div class="col-md-8">
  32. <table class="table table-bordered">
  33. <thead class="alert-info">
  34. <tr>
  35. <th>Full Name</th>
  36. <th>Address</th>
  37. <th>Action</th>
  38. </tr>
  39. </thead>
  40. <tr ng-repeat="member in members">
  41. <td>{{member.name}}</td>
  42. <td>{{member.address}}</td>
  43. <td><button type="button" data-toggle="modal" data-target="#update_member" ng-click="selectMember(member)" class="btn btn-sm btn-warning"><span class = "glyphicon glyphicon-edit"></span> Update</button> | <button type="button" data-toggle="modal" data-target="#delete_member" ng-click = "selectMember(member)" class="btn btn-sm btn-danger"><span class="glyphicon glyphicon-trash"></span> Delete</button></td>
  44. </tr>
  45. </tbody>
  46. </table>
  47. </div>
  48. </div>
  49. <div class="modal fade" id="update_member" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  50. <div class="modal-dialog" role="document">
  51. <div class="modal-content">
  52. <form>
  53. <div class="modal-header">
  54. <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  55. <h4 class="modal-title text-info" id="myModalLabel">Updating Member</h4>
  56. </div>
  57. <div class="modal-body">
  58. <div class = "form-group">
  59. <label>Full Name</label>
  60. <input type = "text" class = "form-control" ng-model = "selectedMember.name"/>
  61. </div>
  62. <div class = "form-group">
  63. <label>Address</label>
  64. <input type = "text" class = "form-control" ng-model = "selectedMember.address"/>
  65. </div>
  66. </div>
  67. <div class="modal-footer">
  68. <button class="btn btn-success" data-dismiss = "modal"><span class = "glyphicon glyphicon-edit"></span> Update</button>
  69. </div>
  70. </form>
  71. </div>
  72. </div>
  73. </div>
  74. <div class="modal fade" id="delete_member" aria-hidden="true">
  75. <div class="modal-dialog">
  76. <div class="modal-content">
  77. <form>
  78. <div class="modal-body">
  79. <center><h4 class="text-danger">Are you sure you want to delete this record?</h4></center>
  80. </div>
  81. <div class="modal-footer">
  82. <button class="btn btn-danger" data-dismiss="modal" ng-click="deleteMember()"><span class="glyphicon glyphicon-check"></span> Yes</button>
  83. <button class="btn btn-success" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> No</button>
  84. </div>
  85. </form>
  86. </div>
  87. </div>
  88. </div>
  89. </body>
  90. <script src="js/angular.js"></script>
  91. <script src="js/script.js"></script>
  92. <script src="js/jquery-3.2.1.min.js"></script>
  93. <script src="js/bootstrap.js"></script>
  94. </html>

Creating the Main Function

This code contains the main function of the application. This code will create, read, delete, update a 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 inside the js directory.
  1. var app = angular.module("myModule", [])
  2. .controller("myController", function($scope){
  3.  
  4. $scope.newMember = {};
  5. $scope.clickedMembers = [];
  6.  
  7.  
  8. $scope.members = [];
  9.  
  10. $scope.saveMember = function(){
  11. $scope.members.push($scope.newMember);
  12. $scope.newMember = {};
  13. };
  14.  
  15. $scope.selectMember = function(member){
  16. $scope.selectedMember = member;
  17. };
  18.  
  19. $scope.deleteMember = function(){
  20. $scope.members.splice($scope.members.indexOf($scope.selectedMember), 1);
  21. };
  22. });
There you have it we successfully created a Create, Read, Delete, Update Functions 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