Basic CRUD Operations Using AngularJS

In this article, we will try to learn how we can use the AngularJS to perform simple CRUD operations, without using any database. We will be using the JavaScript arrays to perform this task.
 
So let's start by creating a new empty project in Visual Studio. We will name it as AngularCRUD.
 
Next, we will remove the web.config file and add an html page named CRUD.html and add reference to online angular js reference. So our html will looks like the following: 
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title></title>  
  5.     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>  
  6. </head>  
  7. <body>  
  8. </body>  
  9. </html>  
Next, we will start with adding the data into a javascript array. For this purpose, we will add two input type controls and and buttons to save data.
Since we need to capture the data for this purpose, we will add a new angular module and define a model named EmpModel in it. It will be having three properties named Id, Name and Salary. We will bind the model with the input controls, by creating a new angular module named mainApp and then a controller named CRUDController. So our code will look like the following: 
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title></title>  
  5.     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>  
  6. </head>  
  7. <body>  
  8.   
  9.     <div ng-app="mainApp" data-ng-controller="CRUDController">  
  10.   
  11.         <table>  
  12.             <tr>  
  13.                 <td>EmpId: </td>  
  14.                 <td>  
  15.                     <span>{{ EmpModel.Id }}</span></td>  
  16.             </tr>  
  17.             <tr>  
  18.                 <td>Name:</td>  
  19.                 <td>  
  20.                     <input type="text" data-ng-model="EmpModel.Name" /></td>  
  21.             </tr>  
  22.             <tr>  
  23.                 <td>Salary:</td>  
  24.                 <td>  
  25.                     <input type="number" data-ng-model="EmpModel.Salary" /></td>  
  26.             </tr>  
  27.             <tr>  
  28.                 <td></td>  
  29.                 <td>  
  30.                     <input type="button" value="Save Data"/></td>  
  31.             </tr>  
  32.         </table>  
  33.     </div>  
  34.   
  35.     <script type="text/javascript">  
  36.         var app = angular.module("mainApp", []);  
  37.   
  38.         app.controller('CRUDController', function ($scope) {  
  39.             $scope.EmpModel = {  
  40.                 Id: 0,  
  41.                 Salary: 0,  
  42.                 Name: '',  
  43.             };   
  44.         });  
  45.     </script>  
  46. </body>  
  47. </html>  
Next, we need to add data into javascript array. So we will add a method named AddData. This will simply get the data from the $scope and add it to the javascript array. So the code of controller code will look like the following: 

  1.        var app = angular.module("mainApp", []);  
  2.        app.controller('CRUDController'function ($scope) {  
  3.   
  4.            $scope.EmpModel = {  
  5.                Id: 0,  
  6.                Salary: 0,  
  7.                Name: '',  
  8.            };  
  9.   
  10.            $scope.EmpList = [];  
  11.            $scope.AddData = function () {  
  12.                var _emp = {  
  13.                    Id: $scope.EmpList.length + 1,  
  14.                    Name: $scope.EmpModel.Name,  
  15.                    Salary: $scope.EmpModel.Salary  
  16.                };  
  17.                $scope.EmpList.push(_emp);  
  18.                ClearModel();  
  19.            }  
  20.   
  21.            function ClearModel() {  
  22.                $scope.EmpModel.Id = 0;  
  23.                $scope.EmpModel.Name = '';  
  24.                $scope.EmpModel.Salary = 0;  
  25.            }  
  26.        });  

Next, we bind the AddData method to the button using ng-click directive and use the ng-repeat directive to display the data from the array in a table. So the html will look like the following: 
  1.        
  2.       <table>  
  3.            <thead>  
  4.                <th>Id</th>  
  5.                <th>Name</th>  
  6.                <th>Salary</th>  
  7.            </thead>  
  8.            <tr data-ng-repeat="Emp in EmpList">  
  9.                <td>{{ Emp.Id }}</td>  
  10.                <td>{{ Emp.Name }}</td>  
  11.                <td>{{ Emp.Salary }}</td>  
  12.            </tr>  
  13.        </table>  

Now, run the application and see the data get's added.
 
 
Next, we will try to delete the record from the table. For this we will add a delete function in our controller to delete the record, for which we press the button. So our code will look like the following:  
  1. $scope.DeleteData = function (emp) {  
  2.                var _index = $scope.EmpList.indexOf(emp);  
  3.                $scope.EmpList.splice(_index, 1);  
  4.            }  
This function will simply receive the argument of the type EmpModel, find its index and remove it from the array. This is because, we will bind the delete button click event with this function using ng-click with the EmpModel instance as the parameter. See the code below: 

  1.     <table border="1" style="width: 300px">  
  2.           <thead>  
  3.               <th>Id</th>  
  4.               <th>Name</th>  
  5.               <th>Salary</th>  
  6.           </thead>  
  7.           <tr data-ng-repeat="Emp in EmpList">  
  8.               <td>{{ Emp.Id }}</td>  
  9.               <td>{{ Emp.Name }}</td>  
  10.               <td>{{ Emp.Salary }}</td>  
  11.               <td>  
  12.                   <input type="button" value="Delete" data-ng-click="DeleteData(Emp)" /></td>  
  13.           </tr>  
  14.       </table>  

Now, run the code and see the results:
 
 
Now finally it's  time to perform the update. For this, we first need to display the data to be updated, in textboxes. For this, we add a method named BindSelectedData in the controller, which will bind the data to the textboxes, through the $scope.EmpModel.
  1. $scope.BindSelectedData = function (emp) {  
  2.     $scope.EmpModel.Id = emp.Id;  
  3.     $scope.EmpModel.Name = emp.Name;  
  4.     $scope.EmpModel.Salary = emp.Salary;  
  5. }  
Bind the method to the table row click using ng-click. So the html mark-up will look like the following:  
  1. <table border="1" style="width: 300px">  
  2.           <thead>  
  3.               <th>Id</th>  
  4.               <th>Name</th>  
  5.               <th>Salary</th>  
  6.           </thead>  
  7.           <tr data-ng-repeat="Emp in EmpList" data-ng-click="BindSelectedData(Emp)">  
  8.               <td>{{ Emp.Id }}</td>  
  9.               <td>{{ Emp.Name }}</td>  
  10.               <td>{{ Emp.Salary }}</td>  
  11.               <td>  
  12.                   <input type="button" value="Delete" data-ng-click="DeleteData(Emp)" /></td>  
  13.           </tr>  
  14.       </table>  
Now select any row and data will be displayed in the textboxes, as they are attached to the $scope.EmpModel, which is further receiving the data from the emp object in the BindSelectedData method. See the screenshot below:
 
 
Next, we add an update button to update the data and add a method named UpdateData in the controller, to perform the update. So our update method will look like the following:
  1. $scope.UpdateData = function () {  
  2.     $.grep($scope.EmpList, function (e) {  
  3.         if (e.Id == $scope.EmpModel.Id) {  
  4.             e.Name = $scope.EmpModel.Name;  
  5.             e.Salary = $scope.EmpModel.Salary;  
  6.         }  
  7.     });  
  8.     ClearModel();  
  9. }  
This method will simply find the record in the array and update it. Bind this method with the click event of the update button we added, using the ng-click directive. So code will look like the following: 
  1. <table>  
  2.           <tr>  
  3.               <td>EmpId: </td>  
  4.               <td>  
  5.                   <span>{{ EmpModel.Id }}</span></td>  
  6.           </tr>  
  7.           <tr>  
  8.               <td>Name:</td>  
  9.               <td>  
  10.                   <input type="text" data-ng-model="EmpModel.Name" /></td>  
  11.           </tr>  
  12.           <tr>  
  13.               <td>Salary:</td>  
  14.               <td>  
  15.                   <input type="number" data-ng-model="EmpModel.Salary" /></td>  
  16.           </tr>  
  17.           <tr>  
  18.               <td>  
  19.                   <input type="button" value="Save Data" data-ng-click="AddData()" /></td>  
  20.               <td>  
  21.                   <input type="button" value="Update Data" data-ng-click="UpdateData()" /></td>  
  22.           </tr>  
  23.       </table>  
Now run the application, edit and record and press Update button and see the data getting updated.
 
 
So our complete html code, including the javascript controller will look like the following:
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title></title>  
  5.     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>  
  6.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>  
  7. </head>  
  8. <body>  
  9.   
  10.     <div ng-app="mainApp" data-ng-controller="CRUDController">  
  11.   
  12.         <table>  
  13.             <tr>  
  14.                 <td>EmpId: </td>  
  15.                 <td>  
  16.                     <span>{{ EmpModel.Id }}</span></td>  
  17.             </tr>  
  18.             <tr>  
  19.                 <td>Name:</td>  
  20.                 <td>  
  21.                     <input type="text" data-ng-model="EmpModel.Name" /></td>  
  22.             </tr>  
  23.             <tr>  
  24.                 <td>Salary:</td>  
  25.                 <td>  
  26.                     <input type="number" data-ng-model="EmpModel.Salary" /></td>  
  27.             </tr>  
  28.             <tr>  
  29.                 <td>  
  30.                     <input type="button" value="Save Data" data-ng-click="AddData()" /></td>  
  31.                 <td>  
  32.                     <input type="button" value="Update Data" data-ng-click="UpdateData()" /></td>  
  33.             </tr>  
  34.         </table>  
  35.   
  36.         <table border="1" style="width: 300px">  
  37.             <thead>  
  38.                 <th>Id</th>  
  39.                 <th>Name</th>  
  40.                 <th>Salary</th>  
  41.             </thead>  
  42.             <tr data-ng-repeat="Emp in EmpList" data-ng-click="BindSelectedData(Emp)">  
  43.                 <td>{{ Emp.Id }}</td>  
  44.                 <td>{{ Emp.Name }}</td>  
  45.                 <td>{{ Emp.Salary }}</td>  
  46.                 <td>  
  47.                     <input type="button" value="Delete" data-ng-click="DeleteData(Emp)" /></td>  
  48.             </tr>  
  49.         </table>  
  50.     </div>  
  51.   
  52.     <script type="text/javascript">  
  53.         var app = angular.module("mainApp", []);  
  54.         app.controller('CRUDController', function ($scope) {  
  55.   
  56.             $scope.EmpModel = {  
  57.                 Id: 0,  
  58.                 Salary: 0,  
  59.                 Name: '',  
  60.             };  
  61.   
  62.             $scope.EmpList = [];  
  63.             $scope.AddData = function () {  
  64.                 var _emp = {  
  65.                     Id: $scope.EmpList.length + 1,  
  66.                     Name: $scope.EmpModel.Name,  
  67.                     Salary: $scope.EmpModel.Salary  
  68.                 };  
  69.                 $scope.EmpList.push(_emp);  
  70.                 ClearModel();  
  71.             }  
  72.   
  73.             $scope.DeleteData = function (emp) {  
  74.                 var _index = $scope.EmpList.indexOf(emp);  
  75.                 $scope.EmpList.splice(_index, 1);  
  76.             }  
  77.   
  78.             $scope.BindSelectedData = function (emp) {  
  79.                 $scope.EmpModel.Id = emp.Id;  
  80.                 $scope.EmpModel.Name = emp.Name;  
  81.                 $scope.EmpModel.Salary = emp.Salary;  
  82.             }  
  83.   
  84.             $scope.UpdateData = function () {  
  85.                 $.grep($scope.EmpList, function (e) {  
  86.                     if (e.Id == $scope.EmpModel.Id) {  
  87.                         e.Name = $scope.EmpModel.Name;  
  88.                         e.Salary = $scope.EmpModel.Salary;  
  89.                     }  
  90.                 });  
  91.                 ClearModel();  
  92.             }  
  93.   
  94.             function ClearModel() {  
  95.                 $scope.EmpModel.Id = 0;  
  96.                 $scope.EmpModel.Name = '';  
  97.                 $scope.EmpModel.Salary = 0;  
  98.             }  
  99.         });  
  100.     </script>  
  101. </body>  
  102. </html>  
Hope you enjoyed reading this article. Happy coding...!
 
Read more articles on AngularJS:

Next Recommended Readings