【问题标题】:Angular Bootstrap Datepicker change month eventAngular Bootstrap Datepicker 更改月份事件
【发布时间】:2015-06-15 15:35:22
【问题描述】:

当用户在 datepicker 日历上更改月份时,我需要调用我的后端,是否可以使用 UI Bootstrap 1.3?

【问题讨论】:

    标签: angularjs datepicker angular-ui-bootstrap


    【解决方案1】:

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

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

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

    【讨论】:

    • 我需要调用服务器以获取当年的假期数组并使用该数组来禁用日期选择器中的日期,我尝试使用您的代码来实现,因为我是新手到 angularjs 装饰器,但是我的控制器中的 $scope.disabled() 函数在 $watch 函数之前执行,如果我可以覆盖 datepicker 的 date-disabled 指令或覆盖 $scope.disabled() 函数,那么也许我可以实现它但不完全知道怎么样,你能帮忙吗
    • 服务器调用完成后可以调用 ctrls[0].refreshView() 更新日期选择器
    • 我收到“未知提供者:datepickerDirectiveProvider”错误,有什么想法吗?
    • 您使用的是哪个版本的 UI Bootstrap?在最近的版本中,该指令被称为“uibDatepicker”而不是“datepicker”
    • 谢谢你,这行得通:)我以前试过,但看起来我拼错了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    • 2019-03-23
    • 2015-06-09
    • 2017-05-19
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    相关资源
    最近更新 更多