2016-09-12 2 views
0

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 개의 questionField 필드가 있고 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; 
} 

}); 

답변

1

안녕하세요 저는 단순한 솔루션으로 스 니펫을 만들었습니다. (저는 다른 간병인을 고려하지 않았습니다.) 질문의 이름 만 번역하고 있지만이 솔루션을 쉽게 확장하여 번역 할 수 있습니다. 필요. 답장을 보내

var json = { 
 
\t "categoryName": "Name of Category", 
 
\t "questions": [ 
 
\t \t { 
 
\t \t \t "questionName": "date", 
 
\t \t \t "questionField": "dateOfInspection", 
 
\t \t \t "questionRequired": "1", 
 
\t \t \t "questionType": "datefield", 
 
\t \t \t "questionLabel": "Date", 
 
\t \t \t "answers": [ 
 
\t \t \t \t  { 
 
\t \t \t \t  "answerName": "date", 
 
\t \t \t \t  "answerType": "datefield" 
 
\t \t \t \t  } 
 
\t \t \t \t ] 
 
\t \t }, { 
 
\t \t \t "questionName": "dealer", 
 
\t \t \t "questionField": "dealer", 
 
\t \t \t "questionRequired": "1", 
 
\t \t \t "questionType": "textfield", 
 
\t \t \t "questionLabel": "Dealer", 
 
\t \t \t "answers": [ 
 
\t \t \t  { 
 
\t \t \t  "answerName": "dealer", 
 
\t \t \t  "answerType": "textfield", 
 
\t \t \t  "answerDescription": "Description Text" 
 
\t \t \t  } 
 
\t \t \t ] 
 
\t \t } 
 
\t ] 
 
} 
 

 
angular.module("app", []) 
 
\t .controller("MainController", MainController); 
 

 
function MainController() { 
 
\t var vm = this; 
 

 
    vm.save = save; 
 
    vm.questions = json.questions; 
 
    vm.translatedQuestions = []; 
 
    
 
    function save() { 
 
    \t var questionsClone = []; 
 
    \t angular.copy(vm.questions, questionsClone); 
 

 
    \t questionsClone.forEach(function (question, index) { 
 
    \t \t angular.extend(question, vm.translatedQuestions[index]); 
 
    \t }); 
 

 
    \t console.log(questionsClone); 
 
    } 
 

 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<div ng-app="app" ng-controller="MainController as main"> 
 
    <form ng-submit="main.save()" novalidate> 
 
    
 
    <div ng-repeat="question in main.questions"> 
 
    <p><b>Name: </b>{{question.questionName}}</p> 
 
    <input type="text" ng-model="main.translatedQuestions[$index].questionName" placeholder="Translate" /> 
 
    </div> 
 

 
    <input type="submit" value="Save" class="center-block" > 
 
    </form> 
 
</div>

+0

고마워요. 그게 내가 찾고 있던거야. – hasan

1

당신이 등등 컨트롤러, 기능 등 충분한 정보를 제공하지 않은,하지만 난 당신에게 당신의 JSON 객체를 생성 할 수있는 경로를 줄 것이기 때문에 그것은 독단적으로 어렵다. 양식 요소에

  1. 당신은, 대신, 나는 'NG 제출 = ctrl.generateObject을'것 '() 제출 = NG 클릭'설정했습니다.

  2. 컨트롤러 안에 빈 개체를 추가하면 응답이 다음과 같이 추가됩니다. '$ scope.answers = {};' 또는 'this.answers = {};'.

    this.generateObject = function(answers){ 
        this.YourObjectToInsert.push(this.answers); 
    } 
    

다시 한번, 당신의 시나리오를 이해하는 것은 매우 어렵습니다,이 라인을 따라하려고 어쩌면합니다

  • , 컨트롤러 내부에서 나는 첫번째 항목에서 언급 한 기능을 확인 당신 자신의 기능. 당신이 크롬을 사용하는 경우

    각도 Batarang 확장을 다운로드하고 당신이 거기에 모든 모델, 응용 프로그램 및 컨트롤러를 볼 수 있도록 AngularJS와에 탭을 탐색 할 브라우저 개발 도구를 사용합니다.

  • +0

    감사합니다. 나는 질문을 편집했다, 지금 그것이 더 명백 할 것이기를 바란다. 하지만 내 주요 문제는 예를 들어, ng-model 내부의'questionField'의 가치를 얻을 수 없다는 것입니다. 'ng-model'을'dateOfInspection'으로 설정하고 모든 필드에 대해 수행해야합니다. – hasan

    관련 문제