हल करें $routeProvider
में $routeProvider
में $http
अनुरोध समाप्त होने पर केवल नए रूट को प्रदर्शित करने का प्रयास कर रहा हूं। अगर अनुरोध सफल होता है, तो $ http.post() से उत्पन्न वादा हल हो जाता है और दृश्य प्रस्तुत किया जाता है। लेकिन अगर अनुरोध विफल रहता है (उदाहरण के लिए टाइमआउट या आंतरिक त्रुटि), वादा कभी हल नहीं होता है और दृश्य कभी प्रस्तुत नहीं किया जाता है। resolve
का उपयोग कर अनुरोध विफलता से मैं कैसे निपट सकता हूं?
कोड के सबसे महत्वपूर्ण भागों bellow है:
app.js
$routeProvider.when('/warrantyResult', {
templateUrl : 'partials/warranty-result.html',
controller : 'WarrantyResultCtrl',
resolve : {
response : [ 'Warranty', function(Warranty) {
return Warranty.sendRequest();
} ]
}
});
controllers.js
angular.module('adocDemo.controllers', []).controller('HomeCtrl', [ '$scope', function($scope) {
} ]).controller('WarrantyCtrl', [ '$scope', '$http', '$location', 'Warranty', function($scope, $http, $location, Warranty) {
$scope.submitWarranty = function() {
$scope.loading = true;
Warranty.setRequestData($scope.data);
$location.path('/warrantyResult');
};
} ]).controller('WarrantyResultCtrl', [ '$scope', 'Warranty', function($scope, Warranty) {
$scope.request = Warranty.getRequestData();
$scope.response = Warranty.getResponseData();
} ]);
services.js
angular.module('adocDemo.services', []).factory('Warranty', [ '$http', '$timeout', function($http, $timeout) {
/**
* This service is used to query the Warranty Webmethod. The sendRequest
* method is automaticcaly called when the user is redirected to
* /warrantyResult route.
*/
var isDataSet = false;
var requestData = undefined;
var responseData = undefined;
return {
setRequestData : function(data) {
//Setting the data
isDataSet = true;
},
getRequestData : function() {
return requestData;
},
sendRequest : function(data) {
if(isDataSet) {
var request = $http.post('url/to/webservice', requestData);
request.success(function(data) {
responseData = data;
});
return request;
}
},
getResponseData : function() {
return responseData;
}
};
} ]);
मुझे पता है मैं एक वादा का उपयोग कर सकता था $ http कॉल के दौर में और अनुरोध विफल होने पर भी इसे हल करें, लेकिन मुझे आश्चर्य है कि कोई आसान समाधान है या नहीं।
पढ़ने और हमें उम्मीद है कि :)
निर्भर करता है कि संकल्प विफल होने पर आप अपने ऐप को कैसे व्यवहार करना चाहते हैं? – Stewie
संकल्प विफल होने पर उपयोगकर्ता को एक अलग मार्ग (/ requestError?) पर रीडायरेक्ट करना सबसे अच्छा व्यवहार होगा। या/वारंटी को प्रस्तुत किया जा सकता है और इस मार्ग का नियंत्रक यह जांचने के लिए प्रभारी होगा कि अनुरोध सफल था या नहीं। – pdegand59