gpt4 book ai didi

javascript - 使用 Angular.JS 将 JSON 导入 HTML(错误)!

转载 作者:行者123 更新时间:2023-12-03 02:36:46 25 4
gpt4 key购买 nike

我在使用 Angular.JS 将 JSON 数据传输为 HTML 时遇到问题,我正在学习编程类(class),这是我遇到的问题,但作者似乎没有遇到过,有人可以帮忙吗?

我正确链接了 Angular ,我正确添加了应用程序,它应该可以工作,有什么想法吗?

https://pastebin.com/K4HR23Tk - HTML

<html ng-app="myQuiz">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Test Your Knowledge: Saturn</title>
<link rel="stylesheet" type="text/css" href="css/quiz.css">
</head>
<body>
<div id="myQuiz" ng-controller="QuizController">
<h1>Test Your Knowledge: <span>Saturn</span></h1>
<div class="progress"> {{totalQuestions}} </div>

https://pastebin.com/5emA4016 -JS

(function(){
var app = angular.module('myQuiz',[]);
app.controller('QuizController',['$scope', '$http', '$sce', function($scope,$http,$sce){
$scope.score = 0;
$scope.activeQuestion = -1;
$scope.activeQuestionAnswered = 0;
$scope.percentage = 0;
$http.get('quiz_data.json').then(function(quizData){
$scope.myQuestions = quizData.data;
$scope.totalQuestions = $scope.myQuestions.length;
});
});
})();

最佳答案

您的代码缺少右方括号 ]。这是完成的代码:

(function(){
var app = angular.module('myQuiz',[]);
app.controller('QuizController',['$scope', '$http', '$sce', function($scope,$http,$sce){
$scope.score = 0;
$scope.activeQuestion = -1;
$scope.activeQuestionAnswered = 0;
$scope.percentage = 0;
$http.get('quiz_data.json').then(function(quizData){
$scope.myQuestions = quizData.data;
$scope.totalQuestions = $scope.myQuestions.length;
}).catch(function(response){
console.error(response && response.message || response);
console.log("Using 2 dummy questions");
$scope.myQuestions = ["dummy", "questions"];
$scope.totalQuestions = $scope.myQuestions.length;
});
// below inserted the missing `]`.
}]);
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html ng-app="myQuiz">
<body ng-controller="QuizController">
<div>Total questions: {{totalQuestions}}</div>
<div ng-repeat="question in myQuestions">{{question}}</div>

您在'QuizController'之后打开的[从未被匹配的]关闭。

如果您使用合适的编辑器或 linter 来帮助突出显示或自动关闭每个 block ,则可以避免此错误。

去过那里的人给你的额外建议(我假设你是一个初学者,因为你在评论中提到了“粘贴”,并且你确实声称你正在参加编程类(class))--如果你正在为这个简单的拼写错误而苦苦挣扎,这意味着你速度太快,没有停下来消化。慢慢来,彻底阅读所有内容并尝试理解它。不要陷入复制和粘贴代码的陷阱。您可能需要更多时间来学习和编写优秀的代码。

祝你编码愉快!

关于javascript - 使用 Angular.JS 将 JSON 导入 HTML(错误)!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48511380/

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