How to set min-time and max-time for Angular Timepicker?

半腔热情 提交于 2019-12-24 03:41:06

问题


Is there a way to set a minimum time and maximum time for Angular's bootstrap time picker? With Angular's Datepicker setting a min and max date is supported, and I am wondering if there is a similar functionality allowed for the Timepicker.

Looking at the settings in the Angular Documentation for Timepicker it does not appear there is a way to do this... but just wondering if anyone is familiar with a way to achieve this behaviour. Thanks in advance for and insights/advice!


Edit:

To give some more background on what I am trying to do:

I have two Timepickers side by side. The one on the left allows a user to select a Start time and the picker on the right allows a user to select an End time. Whichever Start time is selected should set the bottom limit for the End time Timepicker, and whichever End time is selected should set the top limit for the Start time Timepicker.


回答1:


You really only need to set either minTime or maxTime if your validation works correctly, since once your 'start' time is after the 'end' time, the 'end' time is also before 'start' time.

I wanted to do exactly what you're describing and ended up writing a directive to do it.

I picked 'minTime' as my attribute on my 'end' timepicker and set it to the model for my 'start' timepicker. The directive then watches for changes to both models, updates minTime if 'start' time changes, and sets validation on the model based on if 'start' is before 'end'.

You can also use it on input[time] elements as long as ng-model is set.

I tried avoiding $watch at first by using parsers and formatters but it ended up not registering the model as invalid until the next time I changed it.

Here's the directive. Feel free to do wtf you want with it:

directive('minTime', function (){ 
    return {
        require: 'ngModel',
        restrict: 'A',
        link: function(scope, elem, attrs, ctrl) {
            var minTime;

            scope.$watch(attrs.minTime, function(newVal) {
                minTime = newVal;
                validate();
            });

            scope.$watch(attrs.ngModel, validate);

            function validate(value) {
                ctrl.$setValidity('minTime', (minTime < ctrl.$modelValue));
                return value;
            }
        }
    };
})


来源:https://stackoverflow.com/questions/27425976/how-to-set-min-time-and-max-time-for-angular-timepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!