简体   繁体   English

如何从另一个处理程序触发angularjs事件处理程序?

[英]How can I trigger an angularjs event handler from another handler?

I'm using AngularJS 1.3.0-beta.2, jQuery 2.1.0, Angular UI 0.11, and my custom version of Angular UI's Tooltip widget, and I want buttons within my tooltip to close the tooltip when clicked. 我正在使用AngularJS 1.3.0-beta.2,jQuery 2.1.0,Angular UI 0.11和我的自定义版本的Angular UI的工具提示小部件,我希望工具提示中的按钮在单击时可以关闭工具提示。

Plunkr 普伦克

The key part is at crud_tooltip.js:372: 关键部分在crud_tooltip.js:372:

  scope.closePopup = function() {
    var trigger = element.prev();
    if (scope.mode === 'timeout') {
      $timeout(function() {
        trigger.triggerHandler('click');
      });
    }
    else {
      trigger.triggerHandler('click');
    }
  };

The version using $timeout works, but there's a noticeable delay between clicking the button and seeing the popup close. 使用$ timeout的版本可以使用,但是在单击按钮和关闭弹出窗口之间存在明显的延迟。

The version not using $timeout gives an error: [$rootScope:inprog] $apply already in progress . 不使用$ timeout的版本会出现错误: [$ rootScope:inprog] $ apply已经在进行中 It then closes the popup anyway... I'm not sure why. 然后无论如何它都会关闭弹出窗口...我不确定为什么。

How can I modify closePopup (or the ng-click that calls it) to make the tooltip close immediately when the user clicks the button within the tooltip? 当用户单击工具提示中的按钮时,如何修改closePopup(或调用它的ng-click)以立即关闭工具提示?

Note: I adapted my custom_tooltip.js code from Angular UI's tooltip code, using this Plunker as a guideline. 注意:我使用此Plunker作为指导,从Angular UI的工具提示代码改编了custom_tooltip.js代码。 I've also tried directly changing the tt_isOpen value and defining a new crudtooltip-toggle attribute, both with very limited success. 我还尝试过直接更改tt_isOpen值并定义新的crudtooltip-toggle属性,但两者都取得了非常有限的成功。

You're looking at the wrong thing. 您正在看错东西。

That delay is coming from elsewhere and is definitely not bound to the $timeout, but also to the notimeout method (ignoring the error, of course, but that can be easily fixed by checking for $scope.$$phase first). 该延迟来自其他地方,并且绝对不受$ timeout约束,但也不受notimeout方法约束(当然,忽略错误,但是可以通过首先检查$scope.$$phase来轻松解决该错误)。

Also, when you click the original links, both of them, the closing delay is there. 另外,当您单击两个原始链接时,就会出现关闭延迟。

So, in 4 cases you get the same delay, which means it's something in the code. 因此,在4种情况下,您将获得相同的延迟,这意味着代码中存在某些延迟。 I'll give it another look and update the answer if I find what's causing it. 我会给它另一种外观,并在发现问题的原因时更新答案。

Maybe I'm missing the point, but your code seems incredibly complicated and convoluted for such simply functionality. 也许我没有讲到重点,但是您的代码看起来如此复杂,而且功能如此简单,令人费解。 In any case, the delay is actually due to a $timeout which is waiting for some animation to finish. 无论如何,延迟实际上是由于$timeout等待某些动画完成而引起的。 The timeout is triggered because scope.tt_animation evaluates to truthy. 由于scope.tt_animation评估为true,因此触发了超时。 Simply changing the timeout to 0 at line 258 of crud_tooltip.js fixes the issue. 只需在crud_tooltip.js258 crud_tooltip.js超时更改为0 crud_tooltip.js解决此问题。 See this plunk 看到这个pl

Here's the problem area: 这是问题区域:

            if ( scope.tt_animation ) {
              transitionTimeout = $timeout(removeTooltip, 500);
            } else {
              removeTooltip();
            }

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

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