pde*_*d59 13 javascript angularjs
我试图resolve在a $routeProvider中仅在$http请求完成时显示新路由.如果请求成功,则解析$ http.post()产生的承诺并呈现视图.但是如果请求失败(例如,超时或内部错误),则永远不会解析承诺,并且永远不会呈现视图.我如何使用resolve?处理请求失败?
下面代码中最重要的部分是:
app.js
$routeProvider.when('/warrantyResult', {
templateUrl : 'partials/warranty-result.html',
controller : 'WarrantyResultCtrl',
resolve : {
response : [ 'Warranty', function(Warranty) {
return Warranty.sendRequest();
} ]
}
});
Run Code Online (Sandbox Code Playgroud)
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();
} ]);
Run Code Online (Sandbox Code Playgroud)
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;
}
};
} ]);
Run Code Online (Sandbox Code Playgroud)
我知道我可以在$ http调用周围使用promise并解决它,即使请求失败,但我想知道是否有更简单的解决方案.
感谢阅读,并希望,帮助:)
我认为从中做到这一点的唯一方法resolve是手动解决返回的承诺Warranty.sendRequest并将其重新包装在新的承诺中:
resolve : {
response : [ 'Warranty' '$q', function(Warranty, $q) {
var dfd = $q.defer();
Warranty.sendRequest().then(function(result) {
dfd.resolve({ success: true, result : result });
}, function(error) {
dfd.resolve({ success : false, reason : error });
});
return dfd.promise;
} ]
}
Run Code Online (Sandbox Code Playgroud)
在WarrantyResultCtrl,您可以检查是否发生错误并生成重定向.
编辑:更清洁的解决方案:
// WarrantyCtrl
$scope.$on('$routeChangeError', function() {
// handle the error
});
$scope.submitWarranty = function() {
$scope.loading = true;
Warranty.setRequestData($scope.data);
$location.path('/warrantyResult');
};
Run Code Online (Sandbox Code Playgroud)
经过深入搜索,我找不到解决这个问题的方法。
我决定在路由定义中删除解析语句,并在 WarrantyCtrl 中使用以下代码片段。
$scope.submitWarranty = function() {
$scope.formatUserData();
if ($scope.verifyUserData()) {
$scope.loading = true;
Warranty.setRequestData($scope.data);
Warranty.sendRequest().success(function() {
$location.path('/warrantyResult');
}).error(function() {
$location.path('/webserviceError');
});
}
};
Run Code Online (Sandbox Code Playgroud)
不是很聪明,但按预期工作......如果有人仍然有原始问题的解决方案,我会很高兴阅读它!
| 归档时间: |
|
| 查看次数: |
10776 次 |
| 最近记录: |