我正在调用httpService以发布数据并希望显示消息,例如,如果发生错误。
(function () {
'use strict';
angular.module("adminSetup").controller("AccountController", ["$scope", "$location", "httpService", AccountController]);
function AccountController($scope, $location, httpService) {
$scope.message = "";
$scope.error = false;
$scope.user = {
username: '',
password: '',
firstname: '',
lastname: '',
email: '',
city: ''
}
$scope.submitUser = function () {
httpService.registerUser($scope.user,
function (data) {
$scope.message = data;
},
function (error)
{
$scope.message = error;
});;
}
}
})();
服务看起来像这样
(function () {
"use strict"
angular.module("common.services").factory("httpService", ["$q", "$http", "appSettings", userAccountHttp])
function userAccountHttp($q, $http, appSettings) {
var deferred = $q.defer();
var registerUser = function (user) {
$http({
url: appSettings.serverPath + "Account/Register/",
method: "POST",
data: user
}).then(function (data) {
return deferred.resolve("Success");
},
function (response) {
return deferred.reject(response.data.exceptionMessage);
})
}
return {
registerUser: registerUser
};
}
})();
目前,服务未将Http Status消息返回给AccountController。如何将状态“返回”到控制器
答案 0 :(得分:1)
您不需要对承诺建立承诺。这样做:
(function () {
"use strict"
angular.module("common.services").factory("httpService", ["$http", "appSettings", userAccountHttp])
function userAccountHttp($http, appSettings) {
var registerUser = function (user) {
return $http({
url: appSettings.serverPath + "Account/Register/",
method: "POST",
data: user
});
}
return {
registerUser: registerUser
};
});
答案 1 :(得分:1)
我认为这里的问题是你没有回复承诺,你也可以看看this
(function () {
"use strict"
angular.module("common.services").factory("httpService", ["$q", "$http", "appSettings", userAccountHttp])
function userAccountHttp($q, $http, appSettings) {
var deferred = $q.defer();
var registerUser = function (user) {
$http({
url: appSettings.serverPath + "Account/Register/",
method: "POST",
data: user
}).then(function (data) {
deferred.resolve("Success");
},
function (response) {
deferred.reject(response.data.exceptionMessage);
})
return deferred.promise;
}
return {
registerUser: registerUser
};
}
})();