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:
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title></title>
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
- </head>
- <body>
- </body>
- </html>
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:
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title></title>
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
- </head>
- <body>
- <div ng-app="mainApp" data-ng-controller="CRUDController">
- <table>
- <tr>
- <td>EmpId: </td>
- <td>
- <span>{{ EmpModel.Id }}</span></td>
- </tr>
- <tr>
- <td>Name:</td>
- <td>
- <input type="text" data-ng-model="EmpModel.Name" /></td>
- </tr>
- <tr>
- <td>Salary:</td>
- <td>
- <input type="number" data-ng-model="EmpModel.Salary" /></td>
- </tr>
- <tr>
- <td></td>
- <td>
- <input type="button" value="Save Data"/></td>
- </tr>
- </table>
- </div>
- <script type="text/javascript">
- var app = angular.module("mainApp", []);
- app.controller('CRUDController', function ($scope) {
- $scope.EmpModel = {
- Id: 0,
- Salary: 0,
- Name: '',
- };
- });
- </script>
- </body>
- </html>
- var app = angular.module("mainApp", []);
- app.controller('CRUDController', function ($scope) {
- $scope.EmpModel = {
- Id: 0,
- Salary: 0,
- Name: '',
- };
- $scope.EmpList = [];
- $scope.AddData = function () {
- var _emp = {
- Id: $scope.EmpList.length + 1,
- Name: $scope.EmpModel.Name,
- Salary: $scope.EmpModel.Salary
- };
- $scope.EmpList.push(_emp);
- ClearModel();
- }
- function ClearModel() {
- $scope.EmpModel.Id = 0;
- $scope.EmpModel.Name = '';
- $scope.EmpModel.Salary = 0;
- }
- });
- <table>
- <thead>
- <th>Id</th>
- <th>Name</th>
- <th>Salary</th>
- </thead>
- <tr data-ng-repeat="Emp in EmpList">
- <td>{{ Emp.Id }}</td>
- <td>{{ Emp.Name }}</td>
- <td>{{ Emp.Salary }}</td>
- </tr>
- </table>
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:
- $scope.DeleteData = function (emp) {
- var _index = $scope.EmpList.indexOf(emp);
- $scope.EmpList.splice(_index, 1);
- }
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:
- <table border="1" style="width: 300px">
- <thead>
- <th>Id</th>
- <th>Name</th>
- <th>Salary</th>
- </thead>
- <tr data-ng-repeat="Emp in EmpList">
- <td>{{ Emp.Id }}</td>
- <td>{{ Emp.Name }}</td>
- <td>{{ Emp.Salary }}</td>
- <td>
- <input type="button" value="Delete" data-ng-click="DeleteData(Emp)" /></td>
- </tr>
- </table>
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.
- $scope.BindSelectedData = function (emp) {
- $scope.EmpModel.Id = emp.Id;
- $scope.EmpModel.Name = emp.Name;
- $scope.EmpModel.Salary = emp.Salary;
- }

swetha gatlaPosted Mar 19, 2018, 10:10 AM
What is $grep service meant for i above code?
Kishore MadanapaliPosted Jul 4, 2017, 12:28 AM
Can u pls post an example ng-grid for crud operations
Umesh ApPosted Dec 7, 2016, 1:39 AM
Good Article. Can you post CRUD operations using all basic controls (ComboBox, List, Radio Button, CheckBox, DatePicker etc etc) in next article.
Vaibhav DeshmukhPosted Aug 8, 2016, 6:01 AM
Nice One.
Humayun Kabir MamunPosted Apr 24, 2016, 12:44 AM
Nice...
NitinPosted Apr 22, 2016, 11:48 PM
Good one
Bhavik PatelPosted Apr 22, 2016, 10:48 PM
good one
Kuppurasu NagarajPosted Apr 22, 2016, 12:05 PM
Nice Sharing..
Micheal OgunderoPosted Apr 22, 2016, 8:06 AM
Thanks for sharing
Hari ShankerPosted Apr 22, 2016, 6:52 AM
Nice article thanks for sharing
sreenivasa kPosted Apr 22, 2016, 3:33 AM
really nice once
Sanjay GuptaPosted Apr 22, 2016, 1:17 AM
Nice
Jaipal ReddyPosted Apr 21, 2016, 11:56 PM
Good one. .
Debasis SahaPosted Apr 21, 2016, 1:28 PM
Nice One..
Gowtham KPosted Apr 21, 2016, 1:23 PM
Good One, Thanks for sharing:)
Pankaj Kumar ChoudharyPosted Apr 21, 2016, 10:01 AM
Nice Article Sir........
Rahul Kumar SaxenaPosted Apr 21, 2016, 9:03 AM
Good one
Vignesh ManiPosted Apr 21, 2016, 8:22 AM
NIce
Vipul MalhotraPosted Apr 21, 2016, 7:55 AM
Nice article.Thanks for sharing