我正在Angular.js上做一个小测试应用程序。我使用ng-click函数来测试使用JSON文档选择的答案是否正确。当我点击anwser并决定将其更改(真为假,或假为真)时,我得到了一个与Google断开连接的检查目标。
我尝试使用谷歌Chrome,没有任何扩展。
index.html
<!DOCTYPE html>
<html ng-app="quizApp" lang="en">
<head>
<meta charset="UTF-8">
<title>Quiz</title>
<link rel="stylesheet" href="css/style.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-route.min.js"></script>
<script src="app/app.js"></script>
<script src="app/quizCtrl.js"></script>
</head>
<body>
<div class="main">
<div ng-view></div>
</div>
</body>
</html>app.js
var app = angular.module('quizApp', ['ngRoute'])
angular.module('quizApp').config(['$routeProvider', function($routeProvider) {
var routes = [
{
url: '/home',
template: 'templates/quiz.html',
controller: 'quizCtrl'
}
];
routes.forEach(function(r, index) {
$routeProvider.when(r.url, { templateUrl: r.template, controller: r.controller});
});
$routeProvider.otherwise({ redirectTo : '/home' });
}]);quizCtrl.js
app.controller('quizCtrl', function($scope, $http) {
$scope.responses =
$http({
method : 'GET',
url : './data/quiz.json'
}).then(function successCallBack(data) {
$scope.datas = data.data;
}, function errorCallback(data) {
console.log("Error");
});
$scope.result = [];
$scope.isTrue = function(response) {
$scope.json = angular.fromJson(response);
if ($scope.result.length == 0) {
if($scope.json.isTrue) {
$scope.result.push($scope.json.id, true);
}
else {
$scope.result.push($scope.json.id, false);
}
}
else {
console.log($scope.result.length);
for (var i = 0; i < $scope.result.length; i++) {
if ($scope.result[i] == $scope.json.id) {
$scope.result.splice(i, 2);
if($scope.json.isTrue) {
$scope.result.push($scope.json.id, true);
}
else {
$scope.result.push($scope.json.id, false);
}
}
else {
if ($scope.result[i] == true || $scope.result[i] == false) {
console.log("do nothing " + i);
}
else {
if($scope.json.isTrue) {
$scope.result.push($scope.json.id, true);
}
else {
$scope.result.push($scope.json.id, false);
}
}
}
}
}
console.log($scope.result);
}
});我认为我的代码中的错误应该在代码的这一部分中(因为它就在quizCtrl.js的第23行之后崩溃)。
for (var i = 0; i < $scope.result.length; i++) {
if ($scope.result[i] == $scope.json.id) {
$scope.result.splice(i, 2);
if($scope.json.isTrue) {
$scope.result.push($scope.json.id, true);
}
else {
$scope.result.push($scope.json.id, false);
}
}
else {
if ($scope.result[i] == true || $scope.result[i] == false) {
console.log("do nothing " + i);
}
else {
if($scope.json.isTrue) {
$scope.result.push($scope.json.id, true);
}
else {
$scope.result.push($scope.json.id, false);
}
}
}
}发布于 2015-11-27 13:22:12
我认为您的具体问题是,您正在使用$scope.result.length迭代结果,然后在循环中继续推进到该数组。如果注释掉for循环中推送到数组的4行,它不会中断。因此,我认为推进到结果数组,您的迭代是导致问题。
这也许能帮你。
“检查错误断开连接错误”发生在您通过浏览器开发某些网页或CORS请求时,并且您试图访问的文件太大,浏览器无法处理,或者插件干扰了我们的程序(我们正在开发的文件)。
解决方案:尝试禁用所有插件或涉及允许CORS的插件,或者禁用那些提供相同功能的插件(例如,您可能有2-3个插件做相同的工作,允许CORS进行干扰)。检查您正在访问的文件(当然,只有当您正在开发一些web服务、应用程序或一些web开发以便该文件在本地存在时,您才可以签出该文件)。
https://stackoverflow.com/questions/33958154
复制相似问题