角 - UI自举 - 日期选择 - 是否有当月份已经改变检测的方法吗? [英] Angular - ui-bootstrap - datepicker - Is there a way of detecting when the month has changed?
问题描述
我想将日期设置为当前选择每月的第一天,当它改变了。
I'd like to set the date to the 1st of the currently selected month when it's changed.
有没有在该月的监控按钮单击事件的方法吗?
is there a way without monitoring click event on the month's buttons?
TIA
山姆
tia Sam
推荐答案
日期选择的范围有一个属性activeDateId',你可以看。当您切换月它改变了。
Datepicker's scope has a property 'activeDateId' that you can watch. It changes when you switch months.
我认为最好的办法是建立在日期选择器指令一个'装饰',所以你可以把它添加功能。如果重写指令的编译功能,您可以访问它的范围。
I think the best way is to create a 'decorator' on the datepicker directive so you can add functionality to it. You can access its scope if you override the directive's 'compile' function.
您在module.config功能做到这一点:
You do this in a module.config function:
$provide.decorator('datepickerDirective', function($delegate) {
var directive = $delegate[0];
/* Override compile */
var link = directive.link;
directive.compile = function() {
return function(scope, element, attrs, ctrl) {
link.apply(this, arguments);
scope.$watch('activeDateId', function() {
console.log('switched');
});
}
};
return $delegate;
});
修改
我遇到了与如果切换月和2个月有他们在同一天1日起与此类似,其中code的一个问题,在$手表将不会触发。您可以通过在日期选择器的控制器看着财产activeDate'的值解决这个问题:
I ran into an issue with code similar to this where if you switch the month and the 2 months have their 1st date on the same day, the $watch won't fire. You can get around this by watching the value of the property 'activeDate' in Datepicker's controller:
scope.$watch(function() {
return ctrl.activeDate.getTime();
}, function() {
console.log('switched');
});
这篇关于角 - UI自举 - 日期选择 - 是否有当月份已经改变检测的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!