Javascript 对ng模糊应用延迟
我正在使用Javascript 对ng模糊应用延迟,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我正在使用ng focus和ng blur显示/隐藏按钮。在输入的focus上显示按钮,在blur上隐藏按钮。正在使用ng Show执行显示/隐藏。单击此按钮将调用一个函数 问题是,ng blurus首先被调用,而该按钮在触发click事件之前被隐藏,因此从该按钮调用的函数永远不会被调用 我已经通过使用setTimeout()修复了它,但后来发现它并不是一个好的解决方案。是否有其他方法解决此问题?为什么不更改$scope.show=false” 换句话说,删除模糊事件,单击事件将如下所示 $
ng focus
和ng blur
显示/隐藏按钮。在输入的focus
上显示按钮,在blur
上隐藏按钮。正在使用ng Show
执行显示/隐藏。单击此按钮将调用一个函数
问题是,ng blur
us首先被调用,而该按钮在触发click事件之前被隐藏,因此从该按钮调用的函数永远不会被调用
我已经通过使用
setTimeout()
修复了它,但后来发现它并不是一个好的解决方案。是否有其他方法解决此问题?为什么不更改$scope.show=false在按钮的单击事件中选择“代码>”
换句话说,删除模糊事件,单击事件将如下所示
$scope.click = function(){
alert("fuu")
$scope.text = "We changed it";
$scope.show=false;
}
使用ng mouseover
和ng mouseleave
把你的按钮换成
<button ng-click="click()" ng-show="show||mouseover" ng-mouseover="mouseover=true" ng-mouseleave="mouseover=false">Click to change</button>
单击以更改
我认为使用bool可以帮助您确定是否需要隐藏或显示按钮的状态。鼠标悬停在按钮上方时,更改布尔值以确定模糊功能的执行
尝试以下方法:
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>
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;
};
}
使用引入延迟的自定义指令
app.directive('ngBlurDelay',['$timeout',function($timeout){
return {
scope:{
ngBlurDelay:'&'
},
link:function(scope, element, attr){
element.bind('blur',function(){
$timeout(scope.ngBlurDelay,200);
});
}
};
}])
你是在建议我删除ng blur本身吗?我不能删除ng blur。我希望在输入模糊时隐藏该按钮。这样做怎么样?完全删除该按钮并在模糊事件中添加逻辑。@dfsq正如我所说,我可以;哦,我很抱歉,它是固定的,很酷,可以用!但是你能解释一下你在那里做了什么吗?很抱歉耽搁了。我使用ng mouseover和ng mouseleave来确定鼠标何时位于按钮上方。再次抱歉,我的英语太差了!!!棘手的好办法