我有一個包含200個問題和答案的json文件。用戶應該閱讀並翻譯它們。 JSON文件的結構是這樣的:不同的數據綁定方式
categoryName": "Name of Category",
"questions": [
{
"questionName": "date",
"questionField": "dateOfInspection",
"questionRequired": "1",
"questionType": "datefield",
"questionLabel": "Date",
"answers": [
{
"answerName": "date",
"answerType": "datefield"
}
]
},
{
"questionName": "dealer",
"questionField": "dealer",
"questionRequired": "1",
"questionType": "textfield",
"questionLabel": "Dealer",
"answers": [
{
"answerName": "dealer",
"answerType": "textfield",
"answerDescription": "Description Text"
}
]
}
我在頁面上顯示的所有問題的一種形式:
<form name="questionnaireSubmit" action="" id="q" ng-click="submit()" novalidate>
<div ng-repeat="categoryNQuestions in questionnaireFull">
<p id="categoryName" >
{{categoryNQuestions.categoryName}}
</p>
<div ng-repeat="question in categoryNQuestions.questions" id="questionAnswer">
<label id="question">
{{question.questionField}}
</label>
<input type="text" class="form-control" ng-model="ctrl.qs.questionField" name="{{question.questionField}}" placeholder ="{{question.questionField}}" />
<div ng-show="question.questionDescription">
<label id="description">
{{question.questionDescription}}
</label>
<input type="text" class="form-control" name="{{question.questionDescription}}" />
</div>
<input type="submit" value="Save" id="submit_btn" class="center-block" >
</form>
當用戶按下提交按鈕,我想生成與轉化的問題和答案新的JSON對象。 但是雙向數據綁定在這裏不起作用,因爲我有大約200個問題字段字段,並且ng-model
不接受{{questionField}}。
如何以其他方式綁定數據?
我的控制器:
var app = angular.module('questionnaire', []);
app.controller('myQuestionCtrl', function($scope, $location, $http, $window) {
var that=this;
//$scope.question;
$http.get("json_file_url")
.then(function(response) {
$scope.questionnaireFull = {};
$scope.questionnaireFull = response.data.categories;
//create new questionnaire with cleared q_labels, q_descriptions, a_labels and a_descriptions
$scope.questionnaireEmptyDuplicate = angular.copy(response.data);
for (var i = 0; i < $scope.questionnaireEmptyDuplicate.categories.length; i++){
var caregories = $scope.questionnaireEmptyDuplicate.categories[i];
for (var j = 0; j< caregories.questions.length; j++){
var questions = caregories.questions[j];
if (angular.isUndefinedOrNull(questions.questionLabel) === false) {
questions.questionLabel = angular.clearValue(questions.questionLabel);
}
if (angular.isUndefinedOrNull(questions.questionDescription) === false) {
questions.questionDescription = angular.clearValue(questions.questionDescription);
}
angular.forEach(questions.answers, function(answer_value, answer_key) {
if (angular.isUndefinedOrNull(questions.questionDescription) === false) {
answer_value.answerLabel = angular.clearValue(answer_value.answerLabel);
}
if (angular.isUndefinedOrNull(questions.questionDescription) === false) {
answer_value.answerDescription = angular.clearValue(answer_value.answerDescription);
}
})
}
}
console.log($scope.questionnaireEmptyDuplicate);
}); /*end of $http.get*/
$scope.submit = function() {
alert("function is working"); //Add this line to your code to confirm your function is called.
$window.location.href = "https://www.google.de/";
}
//functions
angular.isUndefinedOrNull = function(val) {
return angular.isUndefined(val) || val === null
}
angular.clearValue = function(val){
val = '';
return val;
}
});
謝謝。這就是我一直在尋找的。 – hasan