如何在 AngularJS 上获取 $http readyState?
How to get $http readyState on AngularJS?
如何获取$http readyState,像这样使用:
var request, interval;
request = $http
.post('/user/info', {...})
.success(...);
interval = setInterval(function(){
if(request.readyState < 3)
return;
prepare(request.data, request.headers);
clearInterval(interval)
}, 10);
function prepare(data, headers){
...
}
我不知道如何在不更改 angular.js 文件的情况下执行此操作。是否可以通过 $httpBackend 或其他方式向服务添加一些功能?
有 AngularJS 1.5.5, support was added for additional handling of XHR 个事件:
$http Arguments - Config
Object describing the request to be made and how it should be processed. The object has following properties:
- eventHandlers -
{Object}
- Event listeners to be bound to the XMLHttpRequest object. To bind events to the XMLHttpRequest
upload object, use uploadEventHandlers
. The handler will be called in the context of a $apply
block.
- uploadEventHandlers -
{Object}
- Event listeners to be bound to the XMLHttpRequest upload object. To bind events to the XMLHttpRequest
object, use eventHandlers. The handler will be called in the context of a $apply
block.
使用配置对象的 eventHandlers
属性 添加事件处理程序,获取 XHR readyState
:
The DEMO
angular.module("app",[])
.run(function($rootScope, $http){
var eventHandlers = {readystatechange: readyStateChangeHandler};
var config = { eventHandlers: eventHandlers };
$rootScope.messageList = [];
function readyStateChangeHandler(ev) {
var message = "readyState: "+ev.target.readyState;
console.log(message);
$rootScope.messageList.push(message);
}
$http.get("//httpbin.org/anything",config)
.then(function(response){
console.log("OK");
//console.log(response);
}).catch(function(response){
console.log("ERROR");
//console.log(response);
})
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app">
<div ng-repeat="m in messageList">
{{m}}
</div>
</body>
如何获取$http readyState,像这样使用:
var request, interval;
request = $http
.post('/user/info', {...})
.success(...);
interval = setInterval(function(){
if(request.readyState < 3)
return;
prepare(request.data, request.headers);
clearInterval(interval)
}, 10);
function prepare(data, headers){
...
}
我不知道如何在不更改 angular.js 文件的情况下执行此操作。是否可以通过 $httpBackend 或其他方式向服务添加一些功能?
有 AngularJS 1.5.5, support was added for additional handling of XHR 个事件:
$http Arguments - Config
Object describing the request to be made and how it should be processed. The object has following properties:
- eventHandlers -
{Object}
- Event listeners to be bound to the XMLHttpRequest object. To bind events to theXMLHttpRequest
upload object, useuploadEventHandlers
. The handler will be called in the context of a$apply
block.- uploadEventHandlers -
{Object}
- Event listeners to be bound to the XMLHttpRequest upload object. To bind events to theXMLHttpRequest
object, use eventHandlers. The handler will be called in the context of a$apply
block.
使用配置对象的 eventHandlers
属性 添加事件处理程序,获取 XHR readyState
:
The DEMO
angular.module("app",[])
.run(function($rootScope, $http){
var eventHandlers = {readystatechange: readyStateChangeHandler};
var config = { eventHandlers: eventHandlers };
$rootScope.messageList = [];
function readyStateChangeHandler(ev) {
var message = "readyState: "+ev.target.readyState;
console.log(message);
$rootScope.messageList.push(message);
}
$http.get("//httpbin.org/anything",config)
.then(function(response){
console.log("OK");
//console.log(response);
}).catch(function(response){
console.log("ERROR");
//console.log(response);
})
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app">
<div ng-repeat="m in messageList">
{{m}}
</div>
</body>