AngularJS (54 Blogs) Become a Certified Professional

How to perform CRUD operations in angularjs?

Last updated on Apr 17,2024 34.5K Views


Hey there, if you are looking to learn about CRUD Operations in AngularJS, then this is the right place for you. This article will walk you through CRUD operations in AngularJS and will explain how to use them.

The following topics will be covered in the article

So let’s begin. 

What is AngularJS?
AngularJS is an open-source front end development framework that implements the MVVM (Model-View-ViewModel) architecture. AngularJS is based on JavaScript. It is maintained by Google and arguably one of the most popular frameworks. This is mainly because it enjoys sheer support from Google and incorporates the latest market trends. AngularJS is mostly used to develop single-page applications or SPAs.

Features of AngularJS

  1. MVVM – The MVVM (Model-View-ViewModel) architectural pattern is based on the MVC (Model-View-Control) model. Here, the Controller is replaced by the ViewModel. With this, AngularJS has enabled the concept of two-way data binding. Two-way data binding allows the data to be managed both through models and views.
  2. Data Model Binding – There is no need to write specific code to bind data to HTML controls. This can be achieved by AngularJS by adding just a few snippets of code.
  3. Writing less code – Earlier, for DOM manipulation, a lot of JavaScript was required to be written to design any application. But with AngularJS, very less amount of code is needed for DOM manipulation.
  4. Unit testing ready – Along with AngularJS, the developers of Google also developed a testing framework named “Karma” which helps in designing unit tests for AngularJS applications.

If you wish to learn more about Angular framework, then check out our Online Angular Course which comes with instructor-led live training and real-life project experience. This training will help you understand Angular in-depth and help you achieve mastery over the subject.

Upskill for Higher Salary with Angular Certification Training Course

 

Course NameUpcoming BatchesFees
Angular Certification Training11th May,2024 (Weekend Batch)₹19,995
Angular Certification Training29th June,2024 (Weekend Batch)₹19,995

What is Crud Operations?

CRUD basically stands for Create Read Update Delete data from the server or Database.

How to Implement CRUD operations in AngularJS?

In this tutorial, we will learn to create a demo project for a student management portal which will allow the user to Create, Read(View), Edit(Update) and Delete data using AngularJS. There are many editors in the market today that can be used for angular development e.g Webstorm, Aptana, Sublime Text, Eclipse IDE, etc. You can use any one of them. 

Basic Terms to be known in this tutorial:

  • Controller: This is a regular javascript object which is used to control the data of the AngularJS application.
  • Scope: It is the Binding part between the view and the controller. It is an object with defined properties and methods.

Transform your designs and boost your career with our UI UX Design Course.

Let’s start coding.

STEP1: Create a student.html File :

student.html:

  • Add the below script inside the <head> tag of the html file. It is the CDN link that will help to get started with Angular.

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

  • Create a angularCrud.js file in the same folder and reference the it with the script tag:

<script src="angularCrud.js"></script>

  • Create a Form to Register students.
&lt;div ng-app="myapp" ng-controller="myctrl"&gt; 
&lt;!--ng-app directive is used to initialize an angular application --&gt;
        &lt;label&gt;Name&lt;/label&gt;

        &lt;input type="text" name="name" ng-model="newStudent.name"/&gt;

        &lt;label&gt;Address&lt;/label&gt;

        &lt;input type="text" name="address" ng-model="newStudent.address"/&gt;

        &lt;label&gt;Dept.&lt;/label&gt;

        &lt;input type="text" name="dept" ng-model="newStudent.dept"/&gt;

        

        &lt;input type="hidden" ng-model="newStudent.id" /&gt;

        &lt;input type="button" value="Save" ng-click="saveRecord()" class="btn btn-primary"/&gt;

Note: Here on clicking the button we are calling saveRecord() function which we will create in angularCrud.js file where controller is defined.

  • Create a Table which will display the Data of the students along with options to update and delete students.

&lt;table border="1" bordercolor="blue"&gt;

            
&lt;tr style="color:blue"&gt;

                
&lt;th style="width:150px"&gt;Name&lt;/th&gt;


                
&lt;th style="width:150px"&gt;Address&lt;/th&gt;


                
&lt;th style="width:150px"&gt;Dept&lt;/th&gt;


                    
&lt;th&gt;Action&lt;/th&gt;


            &lt;/tr&gt;


            
&lt;tr style="color:pink" ng-repeat="student in students"&gt;

                
&lt;td&gt;{{ student.name }}&lt;/td&gt;


                
&lt;td&gt;{{ student.address }}&lt;/td&gt;


                
&lt;td&gt;{{ student.dept }}&lt;/td&gt;


                
&lt;td&gt;


                    &lt;a href="#" ng-click="edit(student.id)"&gt;edit&lt;/a&gt; | 

                    &lt;a href="#" ng-click="delete(student.id)"&gt;delete&lt;/a&gt;

                &lt;/td&gt;


            &lt;/tr&gt;


        &lt;/table&gt;

Note:-In the first Row we have added one more heading, “Action”. In the second row, we have added one more column in which two Anchors are used.

Find out our Angular Course in Top Cities

IndiaIndia
Angular Training in BangaloreAngular Training in Delhi
Angular Training in ChennaiAngular Training in Kolkata
Angular Training in HyderabadAngular Training in Mumbai

The first Anchor link is for the “Edit” function and the second Anchor link is for the “Delete” function. The Id of the Student is passed depending on the Anchors, in other words wherever the Anchors are placed, simultaneous to that, the Ids will be passed to the function.

  • Now our View Model e.g students.html is updated and its code is like this:
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;


&lt;img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20src%3D%22https%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.6.9%2Fangular.min.js%22%3E%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&amp;lt;script&amp;gt;" title="&amp;lt;script&amp;gt;" /&gt;
&lt;img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20src%3D%22angularCrud.js%22%3E%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&amp;lt;script&amp;gt;" title="&amp;lt;script&amp;gt;" /&gt;
&lt;title&gt;Student Management Portal&lt;/title&gt;


&lt;/head&gt;
&lt;body&gt;


&lt;div ng-app="myapp" ng-controller="myctrl"&gt;

        &lt;label&gt;Name&lt;/label&gt;

        &lt;input type="text" name="name" ng-model="newStudent.name"/&gt;

        &lt;label&gt;Address&lt;/label&gt;

        &lt;input type="text" name="address" ng-model="newStudent.address"/&gt;

        &lt;label&gt;Dept.&lt;/label&gt;

        &lt;input type="text" name="dept" ng-model="newStudent.dept"/&gt;

        

        &lt;input type="hidden" ng-model="newStudent.id" /&gt;

        &lt;input type="button" value="Save" ng-click="saveRecord()" class="btn btn-primary"/&gt;

        

        

        
&lt;table border="1" bordercolor="blue"&gt;

            
&lt;tr style="color:blue"&gt;

                
&lt;th style="width:150px"&gt;Name&lt;/th&gt;


                
&lt;th style="width:150px"&gt;Address&lt;/th&gt;


                
&lt;th style="width:150px"&gt;Dept&lt;/th&gt;


                    
&lt;th&gt;Action&lt;/th&gt;


            &lt;/tr&gt;


            
&lt;tr style="color:pink" ng-repeat="student in students"&gt;

                
&lt;td&gt;{{ student.name }}&lt;/td&gt;


                
&lt;td&gt;{{ student.address }}&lt;/td&gt;


                
&lt;td&gt;{{ student.dept }}&lt;/td&gt;


                
&lt;td&gt;

                    &lt;a href="#" ng-click="edit(student.id)"&gt;edit&lt;/a&gt; | 

                    &lt;a href="#" ng-click="delete(student.id)"&gt;delete&lt;/a&gt;

                &lt;/td&gt;


            &lt;/tr&gt;


        &lt;/table&gt;


    &lt;/div&gt;


&lt;/body&gt;
 &lt;/html&gt;

 

STEP2: Create an angularCrud.js file which will be our controller.

  • Create an angular module and specify a name to it

var app=angular.module('myapp',[]);

  • Create a controller

app.controller('myctrl',function($scope){});

Now we will create all the CRUD functions one by one

  • CREATE FUNCTION:

$scope.saveRecord = function () {

if ($scope.newStudent.id == null) {

$scope.newStudent.id = empid++;

$scope.students.push($scope.newStudent);

} else {

for (i in $scope.students) {

if ($scope.students[i].id == $scope.newStudent.id) {

$scope.students[i] = $scope.newStudent;

}

}

}

$scope.newStudent = {};

}

The saveRecord() function does two things. One, if it’s a new student we will take the properties from newStudent that came from the template, and push that object in the local address collection. The address collection is then assigned to the model’s collection in the view.

  • EDIT and DELETE FUNCTION:

First, we have provided the delete functionality. For this, we have created a function named “delete”. In this function the Id of the Student is passed, if the Id correctly matches one of the existing Ids then the delete operation will be executed and that entry will be deleted.

Similarly, we have created the “Edit” function. In this function the Id of the Student will also be passed but this time the data related to that Id will not be deleted, instead it will be passed back to the corresponding input boxes so that the user can make changes and when the user clicks the save button the data will again be saved at the same Id position as it was stored previously.

  • Now the Updated angularCrud.js script looks like this –

$scope.delete = function (id) {

for (i in $scope.students) {

if ($scope.students[i].id == id) {

$scope.students.splice(i, 1);

$scope.newStudent = {};

}

}

}

$scope.edit = function (id) {

for (i in $scope.students) {

if ($scope.students[i].id == id) {

$scope.newStudent = angular.copy($scope.students[i]);

}

}

}

Output:

crud operations in angular - edurekaWith this, we have come to the end of this blog on CRUD Operations in AngularJS. I hope reading this blog will help you with your problems related to CRUD operations and makes you capable to use CRUD operations in AngularJS. 

Keep visiting, Keep learning!

A Flutter Training Course is perfect for both beginners and experienced developers who want to learn about cross-platform development.

Got a question for us? Please mention it in the comments section of ” crud operations in  Angularjs?” and I will get back to you.

Upcoming Batches For Angular Course Online with Certification
Course NameDateDetails
Angular Course Online with Certification

Class Starts on 11th May,2024

11th May

SAT&SUN (Weekend Batch)
View Details
Angular Course Online with Certification

Class Starts on 29th June,2024

29th June

SAT&SUN (Weekend Batch)
View Details
Comments
1 Comment
  • RandomPerson says:

    I added

    $scope.students = [];
    var empid = 1;

    inside the jsfile right after app.controller and before $scope.saveRecord.
    And thanks to that the app works as it should.

    The whole js file for me looks like this (in case i missed something, also not that i capitalized ‘myApp’ and ‘myCtrl’:

    var app = angular.module(‘myApp’,[]);
    app.controller(‘myCtrl’,function($scope){
    $scope.students = [];
    var empid = 1;
    $scope.saveRecord = function(){
    if($scope.newStudent.id == null){
    $scope.newStudent.id = empid++;
    $scope.students.push($scope.newStudent);
    }else{
    for(i in $scope.students){
    if($scope.students[i].id == $scope.newStudent.id){
    $scope.students[i] = $scope.newStudent;
    }
    }
    }
    $scope.newStudent={};
    }
    $scope.delete = function(id){
    for(i in $scope.students){
    if($scope.students[i].id == id){
    $scope.students.splice(i,1);
    $scope.newStudent={};
    }
    }
    }
    $scope.edit = function(id){
    for(i in $scope.students){
    if($scope.students[i].id == id){
    $scope.newStudent = angular.copy($scope.students[i]);
    }
    }
    }
    });

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How to perform CRUD operations in angularjs?

edureka.co