Angularjs 如何在angular.js中按日历顺序安排月份?
我使用angular.js作为字体端,node.js作为服务器端 现在,我在数组中随机列出了一些值(月份) HTML代码:Angularjs 如何在angular.js中按日历顺序安排月份?,angularjs,Angularjs,我使用angular.js作为字体端,node.js作为服务器端 现在,我在数组中随机列出了一些值(月份) HTML代码: <div ng-app=""> <div ng-controller="Ctrl"> <select ng-model="month"> <option ng-repeat="month in months">{{month.value}}</option>
<div ng-app="">
<div ng-controller="Ctrl">
<select ng-model="month">
<option ng-repeat="month in months">{{month.value}}</option>
</select>
</div>
</div>
预期产出:
一月二月
四月
我想按照上面的月份顺序在下拉列表中创建一个函数,如下所示
$scope.getMonthValue = function(mon) {
return new Date(Date.parse(mon.value +" 1, 2000")).getMonth()+1;
}
它会将月份名称转换为日期,并返回orderBy筛选器可用于排序的实际月份号
在html中,您可以添加orderBy筛选器:
<select ng-model="month">
<option ng-repeat="month in months | orderBy: getMonthValue">{{month.value}}</option>
</select>
{{month.value}}
不确定是否可能,但对于.Net中的此类内容,我通常使用具有int值和字符串描述的结构。也许这会有帮助…尝试此方法将字符串转换为数字,然后将orderBy添加到ng Repeat感谢您的评论Mario Garcia和Distribute,我将以不同的方式尝试。也许这是一个愚蠢的评论,但您能在控制器代码中对$scope.months进行排序吗?@mgilson$scope.months从DB随机获得值,您的意思是在控制器中以硬代码的形式排列顺序。此结果将在解析函数中打印arrya的反转顺序,但没有属性,这将无法正常工作。我已经更新了答案,可以正常工作了。
<select ng-model="month">
<option ng-repeat="month in months | orderBy: getMonthValue">{{month.value}}</option>
</select>