简体   繁体   English

在ng-blur上应用延迟

[英]Apply delay on ng-blur

I am using ng-focus and ng-blur to show/hide a button. 我正在使用ng-focusng-blur来显示/隐藏按钮。 on focus of an input, a button is shown and on blur it is hidden. 在输入的focus上,显示一个按钮,并且在blur它被隐藏。 Show/hide is being performed using ng-show . 正在使用ng-show执行显示/隐藏。 On click of this button a function gets called. 单击此按钮可调用函数。

Live Demo 现场演示

Issue is that ng-blur us being called first and the button is getting hidden before the click event is fired, hence function which is to be called from that button is never getting called. 问题是ng-blur我们先被调用,而按钮在click事件被触发之前被隐藏,因此从该按钮调用的函数永远不会被调用。

I have already fixed it by using setTimeout() but later found that it is not really a good solution. 我已经使用setTimeout()修复了它,但后来发现它并不是一个很好的解决方案。 Is there any other way to fix this issue? 有没有其他方法可以解决这个问题?

use ng-mouseover and ng-mouseleave change your button to 使用ng-mouseoverng-mouseleave将按钮更改为

        <button ng-click="click()" ng-show="show||mouseover" ng-mouseover="mouseover=true" ng-mouseleave="mouseover=false">Click to change</button>

demo 演示

why don't you change the $scope.show=false; 你为什么不改变$scope.show=false; in the click event of the button. 在按钮的单击事件中。

In other words, remove the blur event, and the click event will be like this. 换句话说,删除模糊事件,click事件将是这样的。

 $scope.click = function(){
    alert("fuu")
    $scope.text = "We changed it";
    $scope.show=false;    
}

I think using a bool can help you to determine the state if it's needed to hide or show the button. 我认为使用bool可以帮助您确定状态,如果需要隐藏或显示按钮。 On mouseover of the button change the bool to determine the execution of blur function. 在鼠标悬停按钮时,更改bool以确定模糊功能的执行。

Try this ways : 试试这个方法:

HTML : HTML:

<div ng-app ng-controller="LoginController">
    <div>{{ text }}</div>
    <input ng-focus="focus()" ng-blur="blur()"></input>
    <button ng-click="click()" ng-show="show==true" ng-mouseover="mouseover()">Click to change</button>
</div>

angularjs : angularjs:

function LoginController($scope) {
    $scope.show=false;
    $scope.blurAll = true;
    $scope.text = "this thing will change on click";

    $scope.focus = function(){
        console.log("buu");
        $scope.show=true;
    }
    $scope.blur = function(){
        if(blurAll){
            console.log("baaa");
            $scope.show=false;
        }
    }

    $scope.click = function(){
            alert("fuu");
            $scope.text = "We changed it";
            $scope.show = false;

    }

    $scope.mouseover = function(){
        blurAll = false;
    };
}

jsFiddle 的jsfiddle

use a custom directive which introduce a delay 使用引入延迟的自定义指令

app.directive('ngBlurDelay',['$timeout',function($timeout){
return {
    scope:{
        ngBlurDelay:'&'
    },
    link:function(scope, element, attr){
    element.bind('blur',function(){
       $timeout(scope.ngBlurDelay,200);
    });
    }
};
}])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM