Subscribe

CRUD with AngularJS

In this simple tutorial we demonstrate the use of AngularJS to perform the CRUD operation on the Data. We are using JSON data to do the demonstration, also Form validation and Array Iteration.

This is an introductory tutorial of crud operations using AngularJS. In this tutorial we are not using any kind of database connectivity or web service, If you are interested in CRUD using restful web service you can look at the Angular+RestEasy(JAX-RS) CRUD Tutorial. In this tutorial We will create a demo book store page which provides a page with input form and table that lists the entries.

The tutorial makes use of JSON notation. We will use a JavaScript array to store and interpret our objects. So lets get started with the HTML part first.

Note: We are assuming that you are familiar with the basics of AngularJS concepts.

We are going to create a simple HTML file and include angular library using CDN in that HTML file, and all of our angular code will reside in a js file called book.js. Initially my HTML code looks like this.

Now we are going to define the initial part of our logic inside the book.js. First of al we are going to make sure our declaration of angular stuff don't pollute the global scope of JavaScript used in the application, its not necessary but is a good practice. After that we need to define the Angular App and controller.

(function(){
...
})();

Now the rest of the code is placed inside this scope. We have defined a variable with name app and given the name "bookStore" to the application. Then we have defined a controller withing the scope of this app by the name of "bookStoreController".

Further more we have defined an array which holds some sample objects, which we are going to use in the example. There are three functions one for each CRUD(read is performed directly on the array) operation along with one extra function which serves to place the values in the form fields for editing. Here you will notice a variable by the name of editId, this is to keep track of the entry which is being updated.

We are using push() to add the newly created object into the array containing the book list and simply assigning the new object to the selected object reference to update, splice to remove an object. $scope is a angular injection which helps to define the scope of the thing in the controller.

Now is the time to fill up the HTML form and to complete the work. Completed HTML code holds a form to create and update the object and a table to list the entries of the array.

Lets Discuss various angular components we have used in the HTML code above.ng-app - To define the application scope.ng-controller - To define controller scope.ng-model - To bind data with view and opposite.$valid - To check the validation of the form.ng-show - To display an entity only when a certain condition is satisfied.ng-hide - To hide an entity only when a certain condition is satisfied.ng-repeat - To repeat over the objects in array.ng-click - To bind function with click event.ng-submit - To bind function with form submit event.

We have used editBook() and deleteBook() function calls with the parameter $index which is actually the index of the elements inside the book array.