Angular2-如何在JSON

时间:2017-01-23 13:03:27

标签: angular typescript angular2-observables

我正在学习angular2并陷入了一个场景,我已经开发了一个表单,我通过JSON填充值。我有几个表单字段和几个文本字段,用户可以在其中输入任何值并选择任何选项。根据选择和输入的值,我想以JSON的形式捕获这些表单值。我想我必须使用Observables但不太确定如何使用它。我怎么能这样做?

以下是我编写的代码。

<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"
       }
     ],                           
   }
 ]

1 个答案:

答案 0 :(得分:0)

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

和ts文件

submitForm(value){
    console.log(value);
}