In the 18th day of AngularJS article series, we are going to be learning the next key players/concept of AngularJS, and understanding the concept of Built in Services of AngularJS. Before moving on to key players/concepts of AngularJS, please read my previous articles:
- Introduction to AngularJS – Day 1
- Introduction to AngularJS – Day 2
- Introduction to AngularJS – Day 3
- Introduction to AngularJS – Day 4
- Introduction to AngularJS – Day 5
- Introduction to AngularJS – Day 6
- Introduction to AngularJS – Day 7
- Introduction to AngularJS – Day 8
- Introduction to AngularJS – Day 9
- Introduction to AngularJS – Day 10
- Introduction to AngularJS – Day 11
- Introduction to AngularJS – Day 12
- Introduction to AngularJS – Day 13
- Introduction To AngularJS – Day 14
- Introduction To AngularJS – Day 15
- Introduction To AngularJS – Day 16
- Introduction to AngularJS – Day 17
AngularJS provides many built in services. For example:

$http Service
The $http service is used to make Ajax call to get the server data.
- $http service provides Ajax functionality.
- Uses the browser's XmlHttpRequest object.
- Makes asynchronous requests.
- Supports multiple HTTP verbs:
• $http.get
• $http.post
• $http.put
• $http.delete
• $http.jsonp
• $http.head
Using this $http service you can access data from any .json file, you can call PHP, Web Api etc.
Syntax:
- /// <reference path="angular.min.js" />
- var app = angular.module("myApp", []);
- //Creating controller & injecteing $http service in controller
- app.controller("myController", function ($scope, $http) {
- In the above we inject the ‘$http’ service and how to use it see following code:
- $scope.init = function () {
- $http({
- method: "GET",
- url: "http://localhost:62039/api/Employees",
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
- }).
- success(function (data, status, headers, config) {
- $scope.employees = data;
- }).
- error(function (data, status, headers, config) {
- });
- };
$http makes asynchronous calls:
- Relies on $q service's deferred/promise APIs
- Access data by calling then() or success()/error()

The above screen shot you can see colored, to access data using success/then.
Example:
In the below demo example we are going to see how we can you ‘$http’ service for CRUD operations. Here we use Web Api to access or save data to database.
app.js
- /// <reference path="angular.min.js" />
- var app = angular.module("myApp", []);
- //Creating controller & injecteing $http service in controller
- app.controller("myController", function ($scope, $http) {
- $scope.update = false;
- //Initialize data
- $scope.init = function () {
- $http({
- method: "GET",
- url: "http://localhost:62039/api/Employees",
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
- }).
- success(function (data, status, headers, config) {
- $scope.employees = data;
- }).
- error(function (data, status, headers, config) {
- });
- };
- //Add Employee
- $scope.addEmployee = function () {
- $scope.para = {
- Id: $scope.temp.id,
- Name: $scope.temp.name,
- Designation: $scope.temp.desg,
- City: $scope.temp.city
- };
- $http.post('http://localhost:62039/api/Employees/PostEmployee', $scope.para).
- success(function (data, status, headers, config) {
- if (data) {
- $scope.temp = {
- id: '',
- name: '',
- desg: '',
- city: ''
- };
- if (!$scope.update) {
- $scope.employees.push(data);
- }
- }
- }).
- error(function (data, status, headers, config) {
- });
- };
- //Update Employee
- $scope.editEmployee = function (emp) {
- $scope.temp = {
- id: emp.Id,
- name: emp.Name,
- desg: emp.Designation,
- city: emp.City
- };
- $scope.index = $scope.employees.indexOf(emp);
- };
- //Delete Employee
- $scope.delEmployee = function (emp) {
- var r = confirm("Are you sure want to delete this employee!");
- if (r == true) {
- $http({
- method: "DELETE",
- url: "http://localhost:62039/api/Employees/" + emp.Id,
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
- }).
- success(function (data, status, headers, config) {
- if (data.success) {
- var index = $scope.employees.indexOf(emp);
- $scope.employees.splice(index, 1);
- }
- }).
- error(function (data, status, headers, config) {
- });
- }
- };
- //Update Employee
- $scope.updateEmployee = function () {
- $scope.update = true;
- $scope.addEmployee();
- };
- });









Kuppurasu NagarajPosted Apr 30, 2016, 2:58 PM
Nice Sharing..
Paramjit SinghPosted Apr 28, 2016, 10:39 AM
It redirects to home page of site.
Paramjit SinghPosted Apr 28, 2016, 10:37 AM
16 :- Introduction To AngularJS – Day 16 : This link is not working
Gowtham KPosted Apr 28, 2016, 5:08 AM
Good One, Thanks for sharing
Sameer RajigarePosted Apr 28, 2016, 1:25 AM
Its really helpful....Very nice article jeetendra....good one
Rahul Kumar SaxenaPosted Apr 27, 2016, 1:25 PM
Good Work
Debasis SahaPosted Apr 27, 2016, 10:13 AM
Good One..
Vivek KumarPosted Apr 27, 2016, 9:28 AM
Good series of article.
Jeetendra GundPosted Apr 27, 2016, 9:15 AM
Thanks all...
Vignesh ManiPosted Apr 27, 2016, 8:21 AM
Good one
Upendra Pratap ShahiPosted Apr 27, 2016, 6:05 AM
nice one...
Gowtham RajamanickamPosted Apr 27, 2016, 4:41 AM
good article..
Manas MohapatraPosted Apr 27, 2016, 4:27 AM
Helpful one..