In my previous post we are getting the list data and showing it in a Table. Even this post has the concept but I used services, and organised the code. Here is my script that gets the Employees list data into an Employees object.

In the view you can use the ng-repeat over these Employees and display employee information.

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

employeesApp.factory('siteUrlBuilder', function($location){
return {
buildSiteUrl: function() {
var urlParts = $location.absUrl().split("/");
return urlParts[0] + "//" + urlParts[2] + "/" + urlParts[3] + "/" + urlParts[4];
}
}
}
);

employeesApp.factory('employeeListService', function($http, siteUrlBuilder){
return {
getEmployees : function(successcb, errorcb)
{
var getSiteUrl = siteUrlBuilder.buildSiteUrl();
$http(
{
method: "GET",
url: getSiteUrl + "/_api/web/lists/getByTitle('Employees')/items",
headers: { "Accept": "application/json;odata=verbose" }
}
).success(function (data, status, headers, config) {
successcb(data.d.results);
}).error(function (data, status, headers, config) {
errorcb(status);
});
}

}
}
);
employeesApp.controller('EmployeesController',
function($scope, $http, employeeListService) {
employeeListService.getEmployees(
function(data)
{
$scope.Employees = data
},
function()
{
alert('error');
}
);
}
);

Happy Coding…!!!

Advertisements