【问题标题】:jQuery key event with angular [duplicate]带有角度的jQuery键事件[重复]
【发布时间】:2015-01-13 16:48:36
【问题描述】:

我有一个查找 Ctrl+C 事件的 jquery 键事件侦听器。我的问题在下面评论。

jQuery(window).on('keydown', function(e){

    if(e.ctrlKey && e.which == '67')
    {
        // console.log('pressed'); This works
        // $scope.closeShareBar(); This works
        $scope.showNotif('The URL has been copied to your clipboard', null, true); // This DOES NOT work
    }

});

$scope.showNotif('The URL has been copied to your clipboard', null, true); // This works

为什么它不起作用? $scope.showNotif() 是我编写的用于显示通知的函数。

编辑
我希望我已经明确表示$scope.closeShareBar(); 可以在没有$apply() 的情况下工作。为什么只有这个工作呢?这不是也有棱角吗?

编辑 2

$scope.showNotif = function(text, status, closeEnabled, quick){
    $scope.notif_text = text;
    $scope.notif = status || 'show';
    if (closeEnabled == true)
    {
        $timeout.cancel($scope.timer);
        $scope.timer = $timeout(function(){
            $scope.notif = '';
        }, 3000);
    }
    if (quick == true)
    {
        $scope.notif = '';
    }
}


$scope.closeShareBar = function(){

    // $scope.shareLink = 'http://localhost:3000/single-signal/'+id;
    angular.element(document.querySelectorAll('.signal_wrapper .signal_data')).removeClass('share_overlay');
    angular.element(document.querySelectorAll('.signal_wrapper .sb_status .share')).removeClass('hide');
    angular.element(document.querySelectorAll('.signal_wrapper .sb_status .close')).addClass('hide');

}

【问题讨论】:

  • 设置调试断点是否到达失败行?
  • 如果您正在寻找一些在 showNotif 中发生的范围绑定更新,您可能需要执行 $scope.$apply()。
  • 只有在更新绑定到视图的范围属性时才需要 scope.apply。没有看到 showNotif 做什么,很难提供帮助。
  • 添加了$scope.showNotif
  • 正如我之前评论过的,您有绑定到在函数中更新的视图的属性,因此您需要在 jquery 事件处理程序中使用 scope.apply。 closeShareBar 之所以有效,是因为您正在直接操作 DOM

标签: javascript jquery angularjs keydown


【解决方案1】:

任何时候你在 Angular 之外做了一些事情,你需要通知它你已经做了改变。所以在你的 $scope.showNotif 之后你需要做一个 $scope.$apply();

$scope.showNotif(...);
$scope.$apply();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 2017-08-25
    • 2015-06-29
    • 2010-12-07
    相关资源
    最近更新 更多