我在这里做了一个简单的http://plnkr.co/edit/zNb65ErYH5HXgAQPOSM0?p=preview
我创建了一个小数据报警器--当你把注意力集中在它之外时,我想要关闭它自己(聚焦于数据报警器)--如果我对输入设置模糊,我无法使用数据报警器,如果我把焦点事件放在数据报警器上,它就不能工作。
我也试过:
angular.element(theCalendar).bind('blur', function () {
$scope.hideCalendar();
});但不起作用。
有线索吗?
发布于 2014-09-10 19:58:38
所以,我知道这可能不是最好的实践,也不是最好的方法,但最后我修复并得到了我需要的东西:
thisInput.bind('focus click', function bindingFunction() {
isMouseOnInput = true;
$scope.showCalendar();
angular.element(theCalendar).triggerHandler('focus');
});
thisInput.bind('blur focusout', function bindingFunction() {
isMouseOnInput = false;
});
angular.element(theCalendar).bind('mouseenter', function () {
isMouseOn = true;
});
angular.element(theCalendar).bind('mouseleave', function () {
isMouseOn = false;
});
angular.element($window).bind('click', function () {
if (!isMouseOn && !isMouseOnInput) {
$scope.hideCalendar();
}
});当你点击页面时,我设置了一些布尔vars来检查鼠标在哪里,如果你有更好的解决方案,请告诉我,它的工作原理就像魅力一样,但这实际上解决了所有问题。
我接受这作为答案,但我感谢所有的人在这一页!
发布于 2014-09-10 15:51:23
这是因为在您有机会做任何事情之前要删除该项,下面是一个有用的示例:
http://plnkr.co/edit/mDfV9NLAQCP4l7wHdlfi?p=preview
只需添加一个超时:
thisInput.bind('blur', function () {
$timeout(function(){
$scope.hideCalendar();
}, 200);
});你考虑过使用现有的内裤吗?像angularUI或角带:http://mgcrea.github.io/angular-strap/##datepickers
更新:
不是一个完整的解决方案,但应该能让您更接近:
angular.element($document[0].body).bind('click', function(e){
console.log(angular.element(e.target), e.target.nodeName)
var classNamed = angular.element(e.target).attr('class');
var inThing = (classNamed.indexOf('datepicker-calendar') > -1);
if (inThing || e.target.nodeName === "INPUT") {
console.log('in');
} else {
console.log('out');
$timeout(function(){
$scope.hideCalendar();
}, 200);
}
});http://plnkr.co/edit/EbQl5xsCnG837rAEhBZh?p=preview
然后,您要做的是监听页面上的单击,如果单击不在日历之外,则关闭它,否则什么也不做。以上只考虑到你点击的东西有一个类名,其中包括数据-日历,你将需要调整它,以便点击在日历内不关闭它。
发布于 2014-09-10 17:34:48
关闭鼠标怎么样?
但是,如果您移动到日历中的另一个div,则需要取消关闭:
//get the calendar as element
theCalendar = element[0].children[1];
// hide the calendar on mouseout
var closeCalendarTimeout = null;
angular.element(theCalendar).bind('mouseout', function () {
if ( closeCalendarTimeout !== null )
$timeout.cancel(closeCalendarTimeout);
closeCalendarTimeout = $timeout(function () {
$scope.hideCalendar();
},250)
});
angular.element(theCalendar).bind('mouseover', function () {
if ( closeCalendarTimeout === null ) return
$timeout.cancel(closeCalendarTimeout);
closeCalendarTimeout = null;
});编辑
向div添加tabindex属性将使其触发焦点和模糊事件。
, htmlTemplate = '<div class="datepicker-calendar" tabindex="0">' +
angular.element(theCalendar).bind('blur', function () {
$scope.hideCalendar();
});https://stackoverflow.com/questions/25769345
复制相似问题