Angularjs从1个选择框开始按月过滤12个月

时间:2015-07-16 15:28:07

标签: javascript angularjs

我有数据我希望过滤这个工作正常,有2个单独的输入,但我试图结合一个选择,月份和年份形成过去12个月。

然后填充服务电话,例如:

url.com?month=1&year=2015

目前我有以下

var date = new Date();
var months = [],
monthNames = [ "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
for(var i = 0; i < 12; i++) {
    months.push(monthNames[date.getMonth()] + ' ' + date.getFullYear());
    date.setMonth(date.getMonth() - 1);
}
$scope.months = months;

填充选择框:

   <select id="transMonth" name="transMonth" ng-model="month" class="form-control" ng-options="currMonth as currMonth for currMonth in months" ng-change="fetchTransactions()">
        <option value="">-- Please select --</option>
   </select>

这目前绑定到单个属性“月”&#39;将值作为月份名称年份(例如2014年1月)我需要将其拆分并更改为数值,即2014年1月

我该怎么做?

1 个答案:

答案 0 :(得分:1)

你可以这样做:

JS:

      var date = new Date();
      var months = [],
      monthNames = [ "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
      for(var i = 0; i < 12; i++) {
          months.push({value: monthNames[date.getMonth()] + ' ' + date.getFullYear(), id: {month: date.getMonth(), year: date.getFullYear()} });
          date.setMonth(date.getMonth() - 1);
      }
$scope.months = months;

HTML:

  <select id="transMonth" name="transMonth" ng-model="month" class="form-control" ng-options="currMonth.id as currMonth.value for currMonth in months" ng-change="fetchTransactions()">
        <option value="">-- Please select --</option>
   </select>
   {{month}}