gpt4 book ai didi

javascript - AngularJS:从表单中获取值(value)

转载 作者:行者123 更新时间:2023-12-03 05:18:04 24 4
gpt4 key购买 nike

我正在使用 3 个嵌套的 ng-repeat 来读取 json 并显示几个问题和他的答案。直到这里它都可以工作,但现在我正在尝试存储所选的答案并将其发送到 API。我不知道为什么所选答案没有被存储。

这是我的观点:

<form>
<div class="panel panel-default" ng-repeat="section in questionsTest">
<div class="panel-heading">
<h1>{{ section.name }}</h1>
<h3 class="panel-title">{{ section.name }}. {{
section.description }}</h3>
</div>
<div class="panel-body" ng-repeat="question in section.questions">
{{ question.statement }}
<div ng-repeat="answer in question.answers">
<!-- <label class="radio-inline"> <input type="{{ answer.type }}" ng-model="question.value"
name="{{ answer.id }}" id="{{ answer.id }}" value="{{ answer.id }}">
{{ answer.valor }}
</label>-->
<label class="radio-inline"> <input type="{{ answer.type }}" ng-model="question.valor"
name="{{ question.id }}" id="{{ answer.id }}" value="{{ answer.id }}">
{{ answer.valor }}
</label>
</div>
</div>
</div>
</form>

这是 Controller :

$scope.question = {};
$scope.testing = function(){
console.log($scope.question);
};

$scope.testing 是一个测试函数,用于在控制台上查看 $scope.question 的值

最佳答案

您的 html 设置是正确的,您只是没有正确读取所选值。要获取第一个问题的选定答案,请使用以下命令:

$scope.json.section[0].questions[0].value

这是因为当你输入question.valor时进入ng-model - 它实际上是一个 n-th里面的问题n-th部分。那些n索引由原始数据结构中的项目数定义 $scope.json .

要获取所有值,您可以迭代原始对象:

$scope.testing = function () {
var answers = {sections: []};

for (var i = 0; i < $scope.json.section.length; i++) {
if (!answers.sections[i]) {
answers.sections[i] = {answers: []};
}

for (var j = 0; j < $scope.json.section[i].questions.length; j++) {
if (!answers.sections[i].answers) {
answers.sections[i].answers = [];
}

answers.sections[i].answers[j] = $scope.json.section[i].questions[j].value
}
}
}

关于javascript - AngularJS:从表单中获取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41525418/

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