2秒後に$リソースのタイムアウトが最小限に抑えられていると苦労しています。おそらく、いくつかの愚かな構文的なもの。
[OK]を、ここに私のjsfiddleです: http://jsfiddle.net/904dykrp/1/
var app = angular.module('app', ['ngResource']);
app.controller('AppController', function($scope, $resource, $timeout) {
var url = "https://httpbin.org/delay/4"; // waits 4 seconds
var myResource = $resource(url, {
method: "get",
timeout: 3000, // 1) timeout after 3s -> gets ignored
isArray: true,
cancellable: true
});
$timeout(function() {
// https://docs.angularjs.org/api/ngResource/service/$resource (at the bottom)
myResource.$cancelRequest(); // 2) how do I cancel?
}, 2000);
myResource.get(function(response) {
$scope.response = response;
}, function(error) {
// Here I want to do sth. with the cancelled request
$scope.error = "Request took too long, cancelled. " + error;
});
});
1)$リソースを(使用...タイムアウト:3000)。これは無視されます。
2)2秒後に$ timeoutを使用してリクエストをキャンセルします。しかし、$ cancelRequestは不明です。
残念ながら、私のリクエストをキャンセルする両方のリクエストは機能しません。
お手伝いできますか?
おかげで、 ベルンハルト
更新(georgeawgによって実施例):
var app = angular.module('app', ['ngResource']);
app.controller('AppController', function($scope, $resource, $timeout) {
var url = "https://httpbin.org/delay/4"; // waits 4 seconds
var myResource = $resource(url, {}, {
timedGet: {
method: "get",
timeout: 3000,
},
});
var x = myResource.timedGet();
x.$promise.then(function(response) {
console.log(response)
$scope.response = response;
}, function(error) {
// Here I want to do sth. with the cancelled request
$scope.error = "Request took too long, cancelled. " + error;
});
});
ありがとうございました。予想どおり、私は近くにいましたが、私とAngularJSのためにしばしば起こるように、仕上げ線に達することができませんでした;-) – Bernie