JavaScript - Remove Array Element Using AngularJS

In this tutorial we will create a Remove Array Element Using AngularJS. This code will automatically remove an array value when the user click the delete button. The code itself use a angular directives that can remove an array value when the array index is set as a parameter in the selectMember() after clicking the button, then it will delete the stored array index in the deleteMember(). 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 - Remove Array Element Using AngularJS</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <table class="table table-bordered">
  18.                         <thead class="alert-info">
  19.                                 <tr>
  20.                                         <th>Member ID</th>
  21.                                         <th>Full Name</th>
  22.                                         <th>Address</th>
  23.                                         <th>Gender</th>
  24.                                         <th>Action</th>
  25.                                 </tr>
  26.                         </thead>
  27.                         <tbody>
  28.                                 <tr ng-repeat="member in members">
  29.                                         <td>{{$index+1}}</td>
  30.                                         <td>{{member.name}}</td>
  31.                                         <td>{{member.address}}</td>
  32.                                         <td>{{member.gender}}</td>
  33.                                         <td><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>
  34.                                 </tr>
  35.                         </tbody>
  36.                 </table>
  37.         </div>
  38.        
  39.         <div class="modal fade" id="delete_member" aria-hidden="true">
  40.                 <div class="modal-dialog">
  41.                         <div class="modal-content">
  42.                                 <form>
  43.                                 <div class="modal-body">       
  44.                                                 <center><h4 class="text-danger">Are you sure you want to delete this record?</h4></center>
  45.                                 </div>
  46.                                 <div class="modal-footer">
  47.                                         <button  class="btn btn-danger" data-dismiss="modal" ng-click="deleteMember()"><span class="glyphicon glyphicon-check"></span> Yes</button>
  48.                                         <button  class="btn btn-success"  data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> No</button>
  49.                                 </div>
  50.                                 </form>
  51.                         </div>
  52.                 </div>
  53.         </div>
  54. </body>
  55. <script src="js/angular.js"></script>
  56. <script src="js/script.js"></script>
  57. <script src="js/jquery-3.2.1.min.js"></script> 
  58. <script src="js/bootstrap.js"></script>
  59. </html>

Creating the Main Function

This code contains the main function of the application. This code delete an array 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.
  1. var app = angular.module("myModule", [])
  2.                                                            .controller("myController", function($scope){
  3.                        
  4.                                                                
  5.                                                                 $scope.members = [
  6.                                                                         {name: "John Smith", address: "New York", gender: "Male"},
  7.                                                                         {name: "Claire Temple", address: "Racoon City", gender: "Female"},
  8.                                                                         {name: "Tont Stank", address: "Japan", gender: "Male"},
  9.                                                                         {name: "Tom Cruise", address: "Tokyo", gender: "Male"}
  10.                                                                 ];
  11.                                                                
  12.                                                                 $scope.selectMember =  function(member){
  13.                                                                         $scope.selectedMember = member;
  14.                                                                 };
  15.                                                                
  16.                                                                 $scope.deleteMember = function(){
  17.                                                                         $scope.members.splice($scope.members.indexOf($scope.selectedMember), 1);
  18.                                                                 };
  19.                                                 });
There you have it we successfully created a Remove Array Element 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