如何使用从angularjs中的$ http响应收到的数组填充选择下拉列表?

时间:2016-09-04 19:12:34

标签: angularjs

我正在尝试使用Months和Day值填充选择下拉列表,这些值是date.json文件中的两个独立数组。我的控制器代码如下

var app = angular.module("profileApp",[]);

app.controller("dobController",function($scope,$http){
$scope.dob={};

$http.get('date.json')
.then(function(response){
    $scope.months=[];
    dob = response.data;
    months=dob.Months;
}, function(response){
     alert("Error in response");
});

我的问题是即使http服务的响应被正确接收(我可以成功地从响应中记录Months数组值),我无法填充我的选择下拉列表并且它仍然是空白的。 HTML如下

<div class="container-fluid" ng-controller="dobController">
  <label class="col-sm-6 control-label">Date of Birth:</label>
    <div class="col-sm-6">
      <select ng-model="birthday" ng-options="option for option in months">{{months}}</select> 
    </div>
</div>

我的date.json文件

{
"Months" : ["January","February","March","April","May","June","July","August","September","October","November","December"],
"Days" : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]
}

1 个答案:

答案 0 :(得分:1)

这样做,

filter_flip

DEMO APP