2

我正在尝试扩展 angular-ui.github.io/bootstrap/datepicker,但它似乎不起作用。

angular.module('myApp',
[
    'ui.bootstrap'
])
.config(function($provide) {
    $provide.decorator('datepickerDirective', function ($delegate) {
        var directive = $delegate[0],
            link = directive.link;
        //works on 1.2
        /*
        angular.extend(directive.scope, {'monthChanged': '&'});
        */
        // Not working in Angular 1.4 too.
        /*directive.$$isolateBindings['monthChanged'] = {
         attrName: 'monthChanged',
         mode: '&',
         optional: true
         };*/
        // Not working in Angular 1.4 too.
        directive.bindToController = {
            'monthChanged': '&'
        };
        directive.compile = function () {
            return function (scope, element, attrs, ctrl) {
                link.apply(this, arguments);

                scope.$watch(function () {
                    return ctrl[0].activeDate.getTime();
                }, function (newVal, oldVal) {
                    if (scope.datepickerMode == 'day') {
                        oldVal = moment(oldVal).format('MM-YYYY');
                        newVal = moment(newVal).format('MM-YYYY');

                        if (oldVal !== newVal) {
                            var arr = newVal.split('-');
                            scope.monthChanged({'$month': arr[0], '$year': arr[1]});
                        }
                    }
                });
            };
        };
        return $delegate;
    })
})
.controller('MyCtrl', ['$rootScope', '$scope',
  function ($rootScope, $scope){
      $scope.changeMonth = function(month, year) {
            console.log(month, year);
            // set your date here if you need to
            // in my case, i needed to request some results via ajax
        };
    }]
);

我不断收到此错误“TypeError:scope.monthChanged 不是函数”我已经调整了来自Angular - ui-bootstrap - datepicker 的代码 - 有没有办法检测月份何时更改?

HTML是:

 <input id="deadlineField" name="deadlineField" type="text" class="form-control"
                           ng-model="newHw.deadline"
                           datepicker-popup="dd MMM yyyy" ng-disabled="viewOnly"
                           is-open="status.calDeadlineOpened" ng-focus="status.calDeadlineOpened=true"
                           show-weeks="false"
                           month-changed="changeMonth($month, $year)"
                            />

试试这个插件:http ://plnkr.co/edit/sXuAkUz0l9XdtdUb457V?p=preview 如果检查代码,当我将内联日历更改为下个月时,会出现以下错误代码: TypeError: scope.monthChanged is not a功能——</p>

4

0 回答 0