简体   繁体   English

triggerHandler导致错误:[$ rootScope:inprog] $ apply已在进行中错误 - AngularJS

[英]triggerHandler causing Error: [$rootScope:inprog] $apply already in progress error - AngularJS

I am trying to trigger the click of a button when a key is pressed. 我按下按键时尝试触发按钮的单击。 I'm doing this using the triggerHandler function, but this is causing the error above. 我正在使用triggerHandler函数执行此操作,但这会导致上述错误。 I'm thinking I must have created some kind of circular reference/loop, but I can't see where. 我想我必须创建某种循环引用/循环,但我看不到哪里。

This is my HTML: 这是我的HTML:

<button id="demoBtn1" hot-key-button hot-key="hotKeys.primaryTest" class="po-btn primary-btn" type="submit" ng-click="btnFunction()"></button>

Here's my controller: 这是我的控制器:

.controller('BtnCtrl', function ($scope) {
    $scope.checkKeyPress = function ($event, hotKeyObj) {
        for(var key in hotKeyObj) {
            if($event.keyCode == hotKeyObj[key].keyCode) {
                var id = hotKeyObj[key].btnId;
                hotKeyObj[key].funcTriggered(id);
            }
        }
    }
    $scope.clickFunction = function(id) {
        var currentButton = angular.element(document.getElementById(id));
        currentButton.triggerHandler("click");
    }
    $scope.btnFunction = function() {
        console.log("clickFunction1 has been triggered");
    }

    $scope.hotKeys = {
        'primaryTest': {
            keyCode: 49,
            keyShortcut: "1",
            label: "Button",
            btnId: 'demoBtn1',
            funcTriggered: $scope.clickFunction
        },
        // more objects here
        }
    }
})

And my directive is here: 我的指示在这里:

.directive("hotKeyButton", function() {
    return {
        controller: 'BtnCtrl',
        scope: {
            hotKey: '='
        },
        transclude: true,
        template: "<div class='key-shortcut'>{{hotKey.keyShortcut}}</div><div class='hotkey-label'>{{hotKey.label}}</div>"
    };
})

It's a bit of a work in progress, so I suspect there might be small errors in places, but I'm primarily interested in the logic running from the keypress to btnFunction being triggered. 这是一项正在进行中的工作,所以我怀疑在某些地方可能存在小错误,但我主要感兴趣的是从按键到触发btnFunction的逻辑运行。 The error fires on the currentButton.triggerHandler("click") line. 错误触发currentButton.triggerHandler("click")行。

Can anyone see what I've done? 谁能看到我做过的事情? Thanks. 谢谢。

Since you have a problem with $apply in progress - you can just wrap your triggerHandler call into $timeout wrapper - just to make everything you need in another $digest -cycle, like this: 由于你在使用$apply遇到问题 - 你可以将你的triggerHandler调用包装到$timeout包装器中 - 只是为了在另一个$digest triggerHandler中创建你需要的所有内容,如下所示:

$scope.clickFunction = function(id) {
    var currentButton = angular.element(document.getElementById(id));
    $timeout(function () {
      currentButton.triggerHandler("click");
    });
}

After this everything will work OK. 在此之后一切都会好起来的。

  • Also don't forget to $inject $timeout service into your BtnCtrl . 另外不要忘记在你的BtnCtrl $inject $timeout服务。
  • Also i'm not sure you need to define controller property for your directive, but it's not a main case. 此外,我不确定您是否需要为您的指令定义controller属性,但它不是主要情况。

Demo: http://plnkr.co/edit/vO8MKAQ4397uqqcAFN1D?p=preview 演示: http//plnkr.co/edit/vO8MKAQ4397uqqcAFN1D?p=preview

暂无
暂无

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

相关问题 角错误:[$ rootScope:inprog] $ apply已在document.click()上进行 - Angular Error: [$rootScope:inprog] $apply already in progress on document.click() $ rootScope:inprog $ apply已在进行中.change事件错误 - $rootScope:inprog $apply already in progress .change event error 获取AngularJS错误:“[$ rootScope:inprog] $ digest已在进行中”没有手动$ apply - Getting AngularJS Error: “[$rootScope:inprog] $digest already in progress” without a manual $apply 错误:$ rootScope:inprog使用angularjs进行的mvc操作已在进行中 - Error: $rootScope:inprog Action Already In Progress mvc with angularjs ($rootScope:inprog) $apply 已经在进行中 - ($rootScope:inprog) $apply already in progress 错误:[$ rootScope:inprog] $ digest已在进行中 - Error: [$rootScope:inprog] $digest already in progress 我没有显式调用$ apply,但仍然得到错误:[$ rootScope:inprog] $ apply已经在进行中 - I'm not calling $apply explicitly but still get Error: [$rootScope:inprog] $apply already in progress 对多个操作同时使用$ apply时出现“错误:[$ rootScope:inprog] $ apply已在进行中” - “Error: [$rootScope:inprog] $apply already in progress” when using $apply for multiple actions occur at the same time 设置“状态”变量的原因错误:$ rootScope:inprog操作已在进行中 - Set “states” variable cause Error: $rootScope:inprog Action Already In Progress 错误:[$ rootScope:inprog] $ digest已在使用angular.copy进行中 - Error: [$rootScope:inprog] $digest already in progress using angular.copy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM