加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS – $cancelRequest在$resource中不可用

发布时间:2020-12-17 17:50:20 所属栏目:安全 来源:网络整理
导读:我试图解雇中止请求,但是我没有在$resource的结果中获得$cancelRequest对象.但是,我能够得到$promise和$resolved对象. 为什么会这样?我怎样才能获得$cancelRequest? PS:我正在使用AngularJS 1.5 更新:经过一些试验和错误后我发现它不起作用只是因为我使
我试图解雇中止请求,但是我没有在$resource的结果中获得$cancelRequest对象.但是,我能够得到$promise和$resolved对象.

为什么会这样?我怎样才能获得$cancelRequest?

PS:我正在使用AngularJS 1.5

更新:经过一些试验和错误后我发现它不起作用只是因为我使用的是AngularJS 1.5 rc 0.现在当我使用AngularJS 1.5 rc 2.这是当前的最新版本时,它只是起作用.

解决方法

根据文档,它仅适用于 Angular 1.5:

$cancelRequest: If there is a cancellable,pending request related to the instance or collection,calling this method will abort the request.

我在Angular 1.4看不到它的任何提及……

我只能建议你更新到1.5版本,但它仍然是一个rc-1版本……

要启用它,您必须配置$resource,默认情况下,它是禁用的:

Hash with custom settings that should extend the default
$resourceProvider behavior. The supported options are:

stripTrailingSlashes – {boolean} – If true then the trailing slashes
from any calculated URL will be stripped. (Defaults to true.)
cancellable – {boolean} – If true,the request made by a
“non-instance” call will be cancelled (if not already completed) by
calling $cancelRequest() on the call’s return value. This can be
overwritten per action. (Defaults to false.)

使用此代码

<html ng-app="test">
<head>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.1/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.1/angular-resource.min.js"></script>

<script>
angular.module('test',['ngResource'])
.config(function($resourceProvider) {
    $resourceProvider.defaults.cancellable = true;
})
.factory('Resource',function($resource) {
    return $resource('api/test',{},{
        test: { cancellable : true }
    });
})
.controller('myController',function($scope,Resource) {
    Resource.query().$cancelRequest(); // ok 
    Resource.test().$cancelRequest(); // ok
});

</script>

</head>
<body ng-controller="myController">

</body>
</html>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读