У меня есть следующий код в сервисе с угловым:
this.login = function(email, password)
{
var promise = $http({
url: 'user/authenticate',
method: "POST",
data: { 'email' : email, 'password' : password }
});
promise.then(
function(payload) {
return true;
}
);
}
Вот мой код:
var loginResult = loginService.login(data.email, data.password);
alert(oginResult);
if (loginResult)
{
$modalInstance.close("");
$state.go("home");
}
else
{
$scope.error = "Login not found.";
}
Я пытаюсь вернуться к вызову service.login. Однако все, что я верну, не определено.
Любая помощь будет оценена, спасибо заранее.
Вам необходимо обновить код. Возвратите обещание от службы и сделайте свою работу в .then в контроллере.
обслуживание
this.login = function(email, password)
{
var promise = $http({
url: 'user/authenticate',
method: "POST",
data: { 'email' : email, 'password' : password }
});
return promise;
}
контроллер
loginService.login(data.email, data.password).then(function(loginResult){
alert(loginResult);
if (loginResult)
{
$modalInstance.close("");
$state.go("home");
}
else
{
$scope.error = "Login not found.";
}
});
Вам нужно создать обещание в вашем SErvice
this.login = function(email, password)
{
// we directly return $http as $http is a promise.
return $http({
url: 'user/authenticate',
method: "POST",
data: { 'email' : email, 'password' : password }
});
}
Управление ответной реакцией в контроллер
// manage the promise return with .then(function() and the response with the parameter of the function
loginService.login(data.email, data.password).then(function(loginResult){
if (loginResult)
{
$modalInstance.close("");
$state.go("home");
}
else
{
$scope.error = "Login not found.";
}
});