Angular Bootstrap Datepicker 更改月份事件 [英] Angular Bootstrap Datepicker change month event

查看:22
本文介绍了Angular Bootstrap Datepicker 更改月份事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用我的后端,当用户在日期选择器日历上更改月份时,UI Bootstrap 1.3 是否可能?

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

推荐答案

您可以使用装饰器扩展 datepicker 指令,并在装饰器中覆盖 compile 函数以添加一个 $watchactiveDate 上.您可以检查日期的月份或年份是否已更改,如果更改,则调用您注入装饰器的服务上的函数.如有必要,传入日期,然后在服务中调用后端.

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.

下面和 fiddle 中的示例 - MyService 只是记录日期,但您可以替换为调用后端.

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;
    }]);
});

这篇关于Angular Bootstrap Datepicker 更改月份事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆