2017-05-01 87 views
0

バックエンドの管理者が質問を追加するシナリオがありますが、どれくらいの数のデバイスを表示する必要があるのか​​分かりません。今度は、私は質問idに従ってこれらの結果を提出しなければなりません。ドロップダウンから選択値を取得AngularJs

これが私の見解コードです:

<div class="list" ng-repeat="question in questions"> 
        <label class="item item-input item-select"> 
         <div class="input-label"> 
          {{question.Question}} 
         </div> 
         <select ng-model="questionID" ng-required="true" ng-options="op as op.name for op in options"> 
         </select> 
        </label> 
        <label class="item item-input"> 
         <textarea placeholder="Comments"></textarea> 
        </label> 
        {{questionID}} - {{question.ID}} //Test: this is to get selected option, value and question id 
       </div> 

[オプション]を選択:

$scope.options = [ 
     { 'name': 'Select', 'value': '' }, 
     { 'name': 'True', 'value': '1' }, 
     { 'name': 'False', 'value': '0' } 
     ]; 

最終的に私はこれらのすべてのような行う必要があります。

"Questions": [ 
    { 
     "QuestionID": 1, 
     "Answer": 1, 
     "Comment" : "Some Comments"  
    }, 
    { 
     "QuestionID": 2, 
     "Answer": 0, 
     "Comment" : "Some Comments" 
    } 
    ] 

アドバイスしてください。ありがとう

答えて

2

コードにいくつかの変更があります。

の1-AnswerQuestionIdCommentsのキーでQuestions配列を定義します。

$indexフォームQuestionsの配列を使用してモデルを定義します。

、3-更新このng-options="op.value as op.name for op in options"

<select ng-model="question.Answer" ng-required="true" ng-options="op as op.name for op in options"> 
    </select> 

<textarea ng-model="question.Comment" placeholder="Comments"></textarea> 

var app = angular.module('anApp', []); 
 
app.controller('ctrl', function($scope) { 
 
    $scope.options = [{ 
 
     'name': 'Select', 
 
     'value': '' 
 
    }, 
 
    { 
 
     'name': 'True', 
 
     'value': '1' 
 
    }, 
 
    { 
 
     'name': 'False', 
 
     'value': '0' 
 
    } 
 
    ]; 
 

 
    $scope.questions = [{ 
 
    "Question": "Question1", 
 
     "QuestionId":1 
 
    }, { 
 
    "Question": "Question2", 
 
     "QuestionId":2 
 
    }]; 
 
    
 
    $scope.Questions = []; 
 

 
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.min.js"></script> 
 

 
<div ng-app="anApp" ng-controller="ctrl"> 
 
    <div class="list" ng-repeat="question in questions"> 
 
     <input type="text" ng-show="false" ng-value="Questions[$index].QuestionId = question.QuestionId"> 
 
    <label class="item item-input item-select"> 
 
         <div class="input-label"> 
 
          {{question.Question}} 
 
         </div> 
 
         <select ng-model="Questions[$index].Answer" ng-required="true" ng-options="op.value as op.name for op in options"> 
 
         </select> 
 
        </label> 
 
    <label class="item item-input"> 
 
         <textarea ng-model="Questions[$index].Comments"></textarea> 
 
        </label> 
 

 
    </div> 
 

 
    {{Questions | json}} 
 
</div>

+2

にこの答えが正しいですが、あなたはまた、彼らは間違って何をしたかOPに説明しなければならないので、彼らはよりよい学ぶことができますあなたの答えから –

+0

質問はIDとquestionTextだけ持っています。 'question.answer'は何も起こりません。私はコントローラの終了について質問していますが、{{questionID}} {{question.ID}} '{" name ":" True "、" value ":" 1 "}から回答に必要なものがすべて選択されています。 - 1.私が望むのは、コントローラの最後にこれらを押し込むことです。 –

+1

更新された回答をご覧ください。 –

関連する問題