我使用了3個嵌套的ng-repeat,用於顯示幾個問題和她的相應答案。直到這裏都很好,但我必須創建一個表單來保存答案,那麼適當的方法是什麼?我與這個測試:從JSON動態表單創建
<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="test.idRadioButton"
name="{{ question.id }}" id="{{ answer.id }}" value="{{ answer.id }}">
{{ answer.valor }}
</label>
</div>
</div>
</div>
</form>
這是JSON:
{
"section": [{
"name": "Sección 1",
"questions": [{
"statement": "Primera pregunta",
"answers": [{
"valor": "1",
"id": 3,
"type": "radio"
}, {
"valor": "2",
"id": 4,
"type": "radio"
}, {
"valor": "3",
"id": 7,
"type": "radio"
}, {
"valor": "4",
"id": 8,
"type": "radio"
}, {
"valor": "5",
"id": 9,
"type": "radio"
}, {
"valor": "6",
"id": 10,
"type": "radio"
}, {
"valor": "7",
"id": 11,
"type": "radio"
}],
"id": 1
}, {
"statement": "Pregunta 3",
"answers": [{
"valor": "Si",
"id": 1,
"type": "radio"
}, {
"valor": "No",
"id": 2,
"type": "radio"
}],
"id": 3
}, {
"statement": "Pregunta 4",
"answers": [{
"valor": "Si",
"id": 1,
"type": "radio"
}, {
"valor": "No",
"id": 2,
"type": "radio"
}],
"id": 4
}],
"description": "description",
"id": 1
}
它不會在所有的工作,當我點擊第一個問題,點擊所有其他問題得到也。我是有角度的新手,我不知道如何面對這個。由於
「工作」了,但我不能存儲所有點擊單選按鈕。這是我有我的控制器:
$scope.question = {};
$scope.testing = function(){
console.log($scope.question);
};
測試功能是作秀存儲在$ scope.question控制檯上的值。
更改ID爲好,不只是名字?也張貼JSON的例子,以幫助破譯你的問題 – MiltoxBeyond
@MiltoxBeyond我已經複製了JSON和試圖改變ID,但仍然無法正常工作 – proktovief