-
使用场景
有个业务需要用户选择月份中的某一天,就是select下拉列表中要有n个选项,分别是1、2、3···31
-
原来的做法
控制器传递过来一个month数组,这个数组里面定义好了多少天,然后一一循环出来
<select> <option value="" ng-repeat='day in month'>{{day.name}}</option> </select>
-
遇到的问题
SPA中有很多个控制器中都要用到这个模块,每次都要传递数据很不方便,重复劳动力
-
我的期望
ng-repeat指令不给定数据源,指令直接循环规定的次数,但是没有成功,就像这样,求各位更加简洁的思路
<select> <option value="" ng-repeat='limitTo:30'>{{$index+1}}</option> </select>
http://www.4byte.cn/question/...
那你把这个功能封装一个自己的directive
效果显然更好,譬如:
app.directive('mydate', function(){
return {
restrict: 'AE',
scope: {
range: '=',
onSelected: '&'
},
link: function($scope, element, attrs){
$scope.month = [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];
$scope.update = function(val){
$scope.onSelected({selected: val});
};
},
template: '<select ng-options="day as day for day in month| limitTo: range" ng-model="select" ng-change="update(select)"></select>'
};
});
在模版里这么使用就好了:
<mydate range="30" on-selected="handler(selected)"></mydate>