How to wrap the datetimepicker js into AngularJS directive

前端 未结 7 2173
陌清茗
陌清茗 2020-12-15 01:59

I have spent sometime on researching the existing datetime directives of angularjs.

Both ngularUI and AngularStrap do not provide a datetimepicker as I needed. Of co

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 02:13

    I ended up putting the input inside of a div and bind model to that. It also includes bootstrapvalidator (Sorry, I didn't have time to make the code perfect, but you should get the idea)

    Sample html code:

    And javascript code:

    app.directive('datetimepicker', function ($timeout) {
    return {
        // Restrict it to be an attribute in this case
        restrict: 'AE',
        // optionally hook-in to ngModel's API 
        require: '?ngModel',
        // responsible for registering DOM listeners as well as updating the DOM
        link: function ($scope, element, $attrs, ngModel) {
            var $element;
            $timeout(function () {
    
                $element = $(element).find("input").datetimepicker($scope.$eval($attrs.datetimepicker));
    
                var DateTimePicker = $element.data("DateTimePicker");
                DateTimePicker.setValueAngular = function (newValue) {
                    this.angularSetValue = true; // a lock object to prevent calling change trigger of input to fix the re-cursive call of changing values
                    this.setDate(newValue);
                    this.angularSetValue = false;
                }
    
                if (!ngModel) { return; }//below this we interact with ngModel's controller
    
                $scope.$watch($attrs['ngModel'], function (newValue) {
                    if (newValue)
                        if (newValue != "Invalid date")
                        {
                            DateTimePicker.setValueAngular(newValue);
                        }
                });
    
                ngModel.$formatters.push(function (value) {
                    // formatting the value to be shown to the user
                    var format = DateTimePicker.format;
                    var date = moment(value);
                    if (date.isValid()) {
                        return date.format(format);
                    }
                    return '';
                });
    
                ngModel.$parsers.push(function toModel(input) {
                    // format user input to be used in code (converting to unix epoch or ...)
                    var modifiedInput = moment(input).format();
                    return modifiedInput;
                });
    
                //update ngModel when UI changes
                $element.on('dp.change', function (e) {
                    if (DateTimePicker.angularSetValue === true)
                        return;
    
                    var newValue = $element[0].value;
                    if (newValue !== ngModel.$viewValue)
                        $scope.$apply(function () {
                            ngModel.$setViewValue(newValue);
                        });
                    //bootstrapvalidator support
                    if ($element.attr('data-bv-field') !== undefined) // if the field had validation
                        $element.closest("form").bootstrapValidator('revalidateField', $element);
    
                });
            });
        }
    };
    }); // directive end
    

提交回复
热议问题