Angular Bootstrap Datepicker change month event

主宰稳场 提交于 2020-01-03 18:45:54

问题


I need to make a call to my backend, when user change month on datepicker calendar is that possible with UI Bootstrap 1.3 ?


回答1:


You can extend the datepicker directive using a decorator, and in the decorator overwrite the compile function to add a $watch on activeDate. You can check if the month or year of the date has changed, and if so call a function on a service that you inject into the decorator. Pass in the date if necessary and in the service perform your call to the backend.

Example below and in fiddle - MyService is just logging the date but you can replace with call to backend.

var app = angular.module('app', ['ui.bootstrap']);

angular.module('app').service('MyService', function() {
    this.doStuff = function(date) {
        console.log(date);
    }
});

angular.module('app').config(function($provide) {
    $provide.decorator('datepickerDirective', ['$delegate', 'MyService', function($delegate, MyService) {
        var directive = $delegate[0];

        var link = directive.link;

        directive.compile = function() {
            return function(scope, element, attrs, ctrls) {
                link.apply(this, arguments);

                scope.$watch(function() {
                    return ctrls[0].activeDate;
                }, function(newValue, oldValue) {
                    if (oldValue.getMonth() !== newValue.getMonth() 
                       || oldValue.getYear() !== newValue.getYear()) {
                        MyService.doStuff(newValue);
                    }
                }, true);
            }
        };
        return $delegate;
    }]);
});


来源:https://stackoverflow.com/questions/30849235/angular-bootstrap-datepicker-change-month-event

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