gpt4 book ai didi

javascript - 解决方法 AngularJS DOM 异常

转载 作者:行者123 更新时间:2023-11-30 05:51:13 28 4
gpt4 key购买 nike

Angular 似乎在 $digest 阶段复制它时被 Chrome(也可能是其他浏览器)的 XMLHttpRequest 的“打开”状态绊倒了。当访问 XMLHttpRequest.status 字段时,它会引发异常。我的实际用例没有绑定(bind)到状态字段; XMLHttpRequest 对象只是我 $watch 数组中另一个对象的一个​​成员。

所以,

a) Angular 是否应该更聪明地处理这种情况并忽略异常?
b) 有没有办法让 Angular 在消化包含对象时忽略字段或对象?
c) 其他解决方法建议?

这是一个例子。这个 HTML+Javascript ( JSFiddle ):

<div ng-app ng-controller="TestCtrl">
<h2>XMLHttpRequest.status: {{ xhr.status }}</h2>
</div>

function TestCtrl($scope) {
$scope.xhr = new XMLHttpRequest();
$scope.xhr.open('POST', 'http://localhost:8000/api/medias', true);
}

导致此错误:

Error: INVALID_STATE_ERR: DOM Exception 11
Error: An attempt was made to use an object that is not, or is no longer, usable.
at eval (eval at Jb (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.0.3/angular.min.js:74:389), <anonymous>:16:4)
at d.fn.x.assign (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.0.3/angular.min.js:63:120)
at Object.j (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.0.3/angular.min.js:52:126)
at Object.e.$digest (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.0.3/angular.min.js:84:286)
at Object.e.$apply (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.0.3/angular.min.js:86:469)
at http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.0.3/angular.min.js:15:396
at Object.d [as invoke] (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.0.3/angular.min.js:26:401)
at pb (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.0.3/angular.min.js:15:317)
at jc (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.0.3/angular.min.js:15:178)
at http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.0.3/angular.min.js:159:424 angular.min.js:60

最佳答案

您需要使用 $http 服务。 (示例 99% 从 Angular 文档示例中提取)

Controller :

function TestCtrl($http, $scope) {
$scope.xhr = {status : undefined};

$http.post('http://localhost:8000/api/medias', {params:{'go':'here'}})
.success(function(data, status) {
$scope.xhr.status = status;
})
.error(function(data, status) {
$scope.xhr.status = status;
});
}

编辑(未测试): Controller 使用XMLHttpRequest:使用 event listeners 调用 $apply

function TestCtrl($http, $scope) {
$scope.xhr = {status : undefined};

var xhr = new XMLHttpRequest();

xhr.addEventListener('progress', function(event) {
var progress = event['something']//get what you need from the event..
$scope.$apply(function() {
$scope.xhr.progress = progress;
});
}, false);

xhr.open('POST', 'http://localhost:8000/api/medias', true);
}

模板:

<div ng-app ng-controller="TestCtrl">
<h2>XMLHttpRequest.status: {{ xhr.status }}</h2>
<h2>XMLHttpRequest.status: {{ xhr.progress}}</h2>
</div>

示例:

关于javascript - 解决方法 AngularJS DOM 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14781845/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com