我正在尝试扩展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不是函数 -