【问题标题】:Prepopulate select option with JSON使用 JSON 预填充选择选项
【发布时间】:2014-09-16 16:29:55
【问题描述】:

我正在尝试使用之前使用 JSON 插入的数据预填充由 angularJS 制作的表单 一切正常,除了未填充的选择选项项

我正在使用的 JSON 示例是:

{
"date": "2014-09-14",
"enumerator": "a",
  "monthreport": {
    "value": "March"
  },
  "weekreport": {
    "value": "2nd week"
  },
  "Maize": 23,
  "Wheat": 41,
  "Sorghum": 71,
  "q14": "Yes"
}

monthreportweekreport 是选择选项,加载表单时不会填写。 q14 是一个单选按钮,它与所有其他输入文本字段一样工作正常,包括文本和数字。 JSON是由angularJS完全生成的,当我在表单中填写数据然后保存时。

在 HTML 中以这种方式指定 select 元素:

<select ng-model="currForm.weekreport" ng-options="o.value for o in options16" name="r_weekreport" required ></select>

并且选项在控制器中设置:

...
$scope.options16= [{value:"1st week"},{value:"2nd week"},{value:"3rd week"},{value:"4th week"},{value:"5th week"}];
...

要加载 JSON,我使用 app.controller 中的常用函数:

...
 $http({
  method: 'GET',
  url: 'http://samplesite.com/formJSON.txt'
}).success(function(data, status) {
   console.log('works!!!   ' + data);
   $scope.currForm = data;  }).error(function(data, status) {
  // Some error occurred
   console.log(status);
})
...

似乎一切正常……我哪里错了??

【问题讨论】:

    标签: ajax json angularjs select


    【解决方案1】:

    你可以试试:

    <select ng-model="currForm.weekreport.value" ng-options="o.value as o.value for o in options16" name="r_weekreport" required ></select>
    

    发生这种情况是因为 Angular 比较了您示例中的对象并且它们不相等。您需要比较原语。

    【讨论】:

    • @Giox :然后尝试:&lt;select ng-model="currForm.weekreport.value" ng-options="o.value as o.value for o in options16" name="r_weekreport" required &gt;&lt;/select&gt;
    • @Giox :我更新了我的答案。您需要比较原语(o - 对象,不是原语,o.value - 原语,String
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 2013-07-16
    • 2018-10-15
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多