将范围功能扩展到Angular 1.4中的指令

时间:2015-10-06 06:25:57

标签: angularjs angularjs-scope angular-ui-bootstrap angularjs-compile angularjs-decorator

我正在尝试扩展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 - Is there a way of detecting when the month has changed?

的代码

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)"
                            />

试试这个plunker:http://plnkr.co/edit/sXuAkUz0l9XdtdUb457V?p=preview 如果您检查代码,当我将内联日历更改为下个月时,会出现以下错误代码:     TypeError:scope.monthChanged不是函数 -

0 个答案:

没有答案