选择标签不以正确的形式返回输入数据(Angular)

时间:2016-04-25 21:12:10

标签: javascript angularjs json

使用我的json文件,我在我的网络应用上制作了选择类型的问题。 我对select标签的代码如下:

<div class="form-group" ng-class="{ 'has-error': form.$submitted && form[field.id].$invalid }" ng-if="field.type === 'select'">
   <label for="{{field.id}}">{{field.title}}</label>
   <br>
   <select ng-model="formData[field.id]" ng-value="{{value.title.id}}" ng-options="value as value.title for value in field.values">
   <option disabled selected value> -- select an option -- </option></select>
   <p class="form-group-note" ng-if="field.info" ng-bind="field.info"></p>
   <div ng-show="form.$submitted" ng-cloack>
      <span class="help-block" ng-show="form['{{field.id}}'].$error.required" ng-if="field.validations.required">Please enter a value, this field is required</span>
   </div>
</div>

我有一个类似以下的json文件:

{
  "groups": [
   {
     "id": "10_8_group",
     "title": "Existence",
     "index": 60,
     "part": 10,
     "sections": [
       {
         "id": "10_8_section",
         "title": "Existence",
         "fields": [
            {
               "id": "10_8_labor_organization",
               "title": "8. Does it exist?",
               "info": "Always select \"No\"",
               "type": "select",
               "size": {
                   "width": 100,
                   "height": 1
               },
               "values": [
                   {
                       "id": 1,
                       "title": "Yes"
                   },
                   {
                       "id": 2,
                       "title": "No. If no, proceed to Part 9. and type or print your explanation."
                   }
               ]
            }
         ]
       }
     ]
   }
  ]
}

当数据保存在localStorage(Angular的前端模型)中时,它将数据保存为"10_8_labor_organization":{"id":1,"title":"Yes"}}

但是,我想将此保存为

"10_8_labor_organization":"Yes"

我怎样才能完成这个?

1 个答案:

答案 0 :(得分:1)

编写代码value.id as value.title for value in field.values