在我的工厂中,我拨打了服务电话。如果我从该调用中得到响应,我想在控制器中执行一个操作(即调用函数doAction())。 虽然我需要一些帮助。由于我的代码现在正在工作,即使服务调用失败,它也会在控制器中执行操作。
如果服务调用失败,代码会进入工厂的Catch部分,但仍返回到控制器,以便达到doAction() - 方法。
我该如何避免这种情况?谢谢你的时间,并原谅一个可能的愚蠢问题。我对Angular很新颖。
在我厂:
app.factory('myFactory', function ($http) {
return {
callService: function() {
return $http.post("http://xxxxx", {}, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
.then(function(response) {
return response.data;
})
.catch(function(response) {
console.error(response.status, response.data);
});
},
};
});
在我的控制器:
var app = angular.module('indexapp', ['ngRoute']);
app.controller('indexController', function($scope, myFactory) {
$scope.makeServiceCall = function() {
var data = myFactory.callService();
data.then(function (result) {
doSomeAction();
});
};
});
谢谢。您的解决方案都可以工 – chichi