Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to return user data from service and set it to the User variable in EmployeeController.

When i debug the code the code i found that call to c# method goes very late.

angular.module('EmployeeServiceModule', [])
.service('EmployeeSer', ['$http',function ($http) {
this.User = '';
this.SearchEmployee = function () {

$http({
method: 'GET',
url: '/Home/GetEmployeeList',
params: { filterData: 'Test' },
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).then(onSuccess, onError);

var onSuccess = function (response) {
this.user = response.data;
this.error = '';
};

var onError = function (reason) {
this.error = "Error in retrieving data.";
};

return this.User;
}
}]);


angular.module('Employee', ['EmployeeServiceModule'])
.controller('EmployeeController', ['EmployeeSer', '$scope', '$http', function (EmployeeSer, $scope, $http) {

this.Id = '';
this.name = '';
this.expertise = '';
$scope.repoSortOrder = 'id';
$scope.filterField = '';

this.GetAllEmployee = function () {
// Initiates the AJAX call


$scope.User = EmployeeSer.SearchEmployee();
// Returns the promise - Contains result once request completes
return true;
};

this.AddEmployee = function () {
var empData = {
Id: $("#txtId").val(),
Name: $("#txtName").val(),
Expertise: $("#expertise").val()
};

$http({
method: 'POST',
url: '/Home/Create',
params: JSON.stringify(empData),
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).then(onSuccess, onError);
// Returns the promise - Contains result once request completes
return true;
};

var onSuccess = function (response) {
$scope.user = response.data;
$scope.error = '';
};

var onError = function (reason) {
$scope.error = "Error in retrieving data.";
};

}]);
Posted
Comments
Simon_Whale 4-Nov-14 5:41am    
what do you mean by the c# method gets called late?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900