gpt4 book ai didi

angularjs - 以 Angular 方式检测窗口焦点?

转载 作者:行者123 更新时间:2023-12-04 10:24:44 26 4
gpt4 key购买 nike

有没有一种很好的 Angular 检测窗口焦点的方法?我正在使用 html5 通知,我只想在窗口失焦时触发。

谢谢!

最佳答案

编辑 @CristiBerceanu 是对的——你应该使用内置的 ng-focus 指令。但是,将此答案作为您要绑定(bind)的任何缺失事件的指南。

您必须创建一个指令:

angular
.module('MyModule', [])
.directive('onFocus', function(){
return {
restrict: 'A',
scope: {
'focus': '&onFocus'
},
link: function($scope, $element, $attributes) {
var focus = function(event) {
$scope.focus({'$event': event});
};
$element.on("focus", focus);
$scope.$on('$destroy', function(){
$element.off('focus', onClick);
});
}
}
});

注意事件是如何被 jquery 绑定(bind)在指令中的,而不是直接在 Controller 中。此外,请注意绑定(bind)表达式使用 & 绑定(bind)。前缀(可评估的表达式绑定(bind))而不是像 @ 这样的常规前缀(文本绑定(bind))或 = (范围属性引用,双向,绑定(bind))。

关于angularjs - 以 Angular 方式检测窗口焦点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27789326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com