【问题标题】:Angularjs filter by month by the last 12 months from 1 select boxAngularjs 从 1 个选择框中按过去 12 个月按月过滤
【发布时间】:2015-07-16 23:43:10
【问题描述】:

我有想要过滤的数据,使用 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>

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

我该怎么做?

【问题讨论】:

  • 也许将months 更改为对象数组?对象可能类似于 {month: 1, year: 2014}。

标签: javascript angularjs


【解决方案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}}

【讨论】:

  • 似乎越来越接近我目前需要的 $scope.month 和 $scope.year 是 api 的 url 中的值,即 url.com?month=$scope.month&year=$scope.year我可以拆分此结果以便将值传递给这些结果吗?我可能需要将 ng-model 更改为其他内容。
  • 你可以做url.com?month=$scope.month.month&amp;year=$scope.month.year
猜你喜欢
  • 2022-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-26
  • 1970-01-01
  • 2018-12-06
  • 2020-10-09
相关资源
最近更新 更多