2017-01-23 6 views
1

私はangular2を学習しており、JSONを通じて値を入力するフォームを開発したシナリオに悩まされています。いくつかのフォームフィールドと、ユーザーが任意の値を入力して任意のオプションを選択できるテキストフィールドはほとんどありません。選択して入力した値に基づいて、これらのフォーム値をJSON形式で取り込みたいと思います。私はObservablesを使用しなければならないと思うが、その使用方法はあまりよく分かっていないと思う。それはどうすればいいのだろうか?Angular2- JSONで選択したフォームフィールドを抽出/保存/キャプチャする方法

以下は私が書いたコードです。

<ul> 
    <li> 
    <div *ngFor="let question of questions> 
     <div class="row"> 
      <div class="col-md-12"> 
      <md-input placeholder="{{question.displayKey }}"></md-input> 
      </div> 
     </div> 
     <div class="row row-bordered"> 
      <div class="col-md-8"> {{question.displayKey }}</div> 
      <div class="col-md-4"> 
      <md-radio-group> 
       <span> 
       <md-radio-button *ngFor="let option of question.choices" name="{{option.displayKey}}" [value]="option.displayKey" aria-label="Yes" tabindex="0">{{option.displayKey}}</md-radio-button> 
       </span> 
      </md-radio-group> 
      </div>        
     </div>       
    </div> 
    </li> 
</ul> 

JSON-

"questions": [ 
    { 
    "code": "12345", 
    "displayKey": "Question1?", 
    "required": true, 
    "questionType": "Boolean", 
    "choices": [ 
     { 
     "choiceCode": "true", 
     "displayKey": "Yes" 
     }, 
     { 
     "choiceCode": "false", 
     "displayKey": "No" 
     } 
    ],       
    }, 
    { 
    "code": "aw345y", 
    "displayKey": "Question2?", 
    "required": true, 
    "questionType": "Boolean", 
    "choices": [ 
     { 
     "choiceCode": "true", 
     "displayKey": "Yes" 
     }, 
     { 
     "choiceCode": "false", 
     "displayKey": "No" 
     } 
    ],       
    } 
] 
+0

テンプレートフォーム(ngModel)のいずれかを使用したら、あなたは "自由のために" この機能を取得する必要がありますかリアクティブフォーム(FormBuilder)。これはフォームの「価値」です。 – Zyga

答えて

0

<form #form="ngForm" (ngSubmit)="submitForm(form.value)">

とTSファイルで

submitForm(value){ 
    console.log(value); 
} 
関連する問題