在angularJs中从Service调用ajax时,它会在执行函数后转到服务器。 [英] When calling ajax from Service in angularJs it go to server after execution of function.
问题描述
我想从服务返回用户数据并将其设置为EmployeeController中的User变量。
当我调试代码时,我发现调用c#的代码方法很晚。
angular.module('EmployeeServiceModule',[])
.service('EmployeeSer',['$ http',函数($ http){
this.User ='';
this.SearchEmployee = function(){
$ http({
方法:'GET',
url:'/ Home / GetEmployeeList',
params :{filterData:'Test'},
标题:{'Content-Type':'application / x-www-form-urlencoded'}
})。然后(onSuccess,onError);
var onSuccess = function(response){
this.user = response.data;
this.error ='';
};
var onError = function(reason){
this.error =检索数据时出错。;
};
返回此。用户;
}
}]);
< br $>
angular.module('Employee',['EmployeeServiceModule'])
.controller('EmployeeController',['EmployeeSer',' $ scope','$ http',函数(EmployeeSer,$ scope,$ http){
this.Id ='';
this.name ='';
this.expertise ='';
$ scope.repoSortOrder ='id';
$ scope .filterField ='';
this.GetAllEmployee = function(){
//启动AJAX调用
$ scope.User = EmployeeSer.SearchEmployee();
//返回承诺 - 请求完成后包含结果
返回true;
};
this.AddEmployee = function(){
var empData = {
Id:$(#txtId)。val(),
名称:$(#txtName)。val(),
专长:$(#exp ertise)。val()
};
$ http({
方法:'POST' ,
url:'/ Home / Create',
params:JSON.stringify(empData),
header:{'Content-Type ':'application / x-www-form-urlencoded'}
})。then(onSuccess,onError);
//返回promise - 包含一次请求的结果完成
返回true;
};
var onSuccess = function(response){
$ scope.user = response.data;
$ scope.error ='';
};
var onError = function(reason){
$ scope.error =检索数据时出错。;
};
}]);
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.";
};
}]);
推荐答案
http',function(
http',function (
http){
this.User ='';
this.SearchEmployee = function(){
http) {
this.User = '';
this.SearchEmployee = function () {
>
http({
m ethod:'GET',
url:'/ Home / GetEmployeeList',
params:{filterData:'Test'},
header :{'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 =检索数据时出错。;
};
返回此。用户;
}
}]); < br $>
angular.module('Employee',['EmployeeServiceModule'])
.controller('EmployeeController) ',['EmployeeSer','
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', '
这篇关于在angularJs中从Service调用ajax时,它会在执行函数后转到服务器。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!