Javascript AngularJS工厂函数添加承诺
我在angular factory中创建一个函数。我需要这个函数返回承诺。然后我添加了其他功能Javascript AngularJS工厂函数添加承诺,javascript,angularjs,angular-promise,Javascript,Angularjs,Angular Promise,我在angular factory中创建一个函数。我需要这个函数返回承诺。然后我添加了其他功能 SPApp.factory('processing', ['$http', '$rootScope', '$q', function ($http, $rootScope) { function initialize($scope, status) { $http({ method: 'GET', url: '/api/ApiCust
SPApp.factory('processing', ['$http', '$rootScope', '$q', function ($http, $rootScope) {
function initialize($scope, status) {
$http({
method: 'GET',
url: '/api/ApiCustomer/GetCustomerDetails'
}).then(function (data) {
if (data.data.ModelValue == null) {
$rootScope.redirectToLogin();
} else {
$scope.CustomerInformation = data.data.ModelValue;
}
}).then(function () {
$http({
method: 'GET',
url: '/api/ApiList/AccountTypeList'
}).success(function (result) {
$scope.accountTypeList = result;
typeOfAccount = result;
});
// get currncy list
$http({
method: 'GET',
url: '/api/ApiList/CurrencyList'
}).success(function (result) {
$scope.currencyList = result;
//jQuery('#ProfileImage').attr('src', "/Content/Images/blank-avatar.jpg");
});
$http({
method: 'GET',
url: '/api/ApiList/BankListByUserType',
}).success(function (result) {
$scope.bankList = result;
});
});
};
return {
initialize: initialize
}
}]);
那么如何调用这个函数作为承诺和使用呢
processing.initialize($scope, true).then(function(){
console.log("hello world");
});
现在plz帮助不起作用了,如果您知道流程,您应该从工厂返回
Promise
SPApp.factory('processing', ['$http', '$rootScope', '$q', function ($http, $rootScope, $q) {
function initialize($scope, status) {
return $http({
method: 'GET',
url: '/api/ApiCustomer/GetCustomerDetails'
});
}
return {
initialize: initialize
}
}
]);
控制器方法中的消耗输出
processing.initialize($scope, true).then(function (data) {
$scope.basicInfo = data.data.ModelValue.PersonalInformations[0];
});
根据注释初始化函数有多个
http
请求,您可以使用$q.all()
方法,该方法将等待所有承诺完成
SPApp.factory('processing', ['$http', '$rootScope', '$q', function ($http, $rootScope, $q) {
function initialize($scope, status) {
var returnData = {},
var a = $http({
method: 'GET',
url: '/api/ApiCustomer/GetCustomerDetails'
}).then(function (response) {
returnData.basicInfo = response.data.ModelValue.PersonalInformations[0];
});
var b = $http({
method: 'GET',
url: '/api/ApiList/AccountTypeList'
}).then(function (response) {
returnData.accountTypeList = response;
});
return $q.all([a, b]).then(function () {
return returnData;
});
}
return {
initialize: initialize
}
}
]);
您应该从工厂返回
Promise
SPApp.factory('processing', ['$http', '$rootScope', '$q', function ($http, $rootScope, $q) {
function initialize($scope, status) {
return $http({
method: 'GET',
url: '/api/ApiCustomer/GetCustomerDetails'
});
}
return {
initialize: initialize
}
}
]);
控制器方法中的消耗输出
processing.initialize($scope, true).then(function (data) {
$scope.basicInfo = data.data.ModelValue.PersonalInformations[0];
});
根据注释初始化函数有多个
http
请求,您可以使用$q.all()
方法,该方法将等待所有承诺完成
SPApp.factory('processing', ['$http', '$rootScope', '$q', function ($http, $rootScope, $q) {
function initialize($scope, status) {
var returnData = {},
var a = $http({
method: 'GET',
url: '/api/ApiCustomer/GetCustomerDetails'
}).then(function (response) {
returnData.basicInfo = response.data.ModelValue.PersonalInformations[0];
});
var b = $http({
method: 'GET',
url: '/api/ApiList/AccountTypeList'
}).then(function (response) {
returnData.accountTypeList = response;
});
return $q.all([a, b]).then(function () {
return returnData;
});
}
return {
initialize: initialize
}
}
]);
你应该
返回
承诺而不是仅仅执行它$scope
$q
服务似乎未使用(不确定您的计划) SPApp.factory('processing', ['$http', '$rootScope', function ($http, $rootScope) {
function initialize(status) {
return $http({
method: 'GET',
url: '/api/ApiCustomer/GetCustomerDetails'
}).then(function (data) {
return data.data.ModelValue.PersonalInformations[0];
}
}
return {
initialize: initialize
}
}]);
用法
你应该
返回
承诺而不是仅仅执行它$scope
$q
服务似乎未使用(不确定您的计划) SPApp.factory('processing', ['$http', '$rootScope', function ($http, $rootScope) {
function initialize(status) {
return $http({
method: 'GET',
url: '/api/ApiCustomer/GetCustomerDetails'
}).then(function (data) {
return data.data.ModelValue.PersonalInformations[0];
}
}
return {
initialize: initialize
}
}]);
用法
请参阅下面的链接,了解angular中的promise函数,这有助于解决此问题
请参阅下面的链接,了解angular中的promise函数,这有助于解决此问题