私の工場ではサービスコールをします。私がその呼び出しから応答を受け取った場合、私は自分のコントローラでアクションを実行したい(つまり、関数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