0

我在一个有角度的单页应用程序中使用 smarty street jquery.liveaddress 插件。当我转换到下一页时,我需要从 liveaddress 实例中取消绑定所有事件侦听器。现在,如果我回到页面,所有事件都会被触发两次,然后下一次在页面上触发 3 次。

我已经尝试了我能想到的一切。我的最后一个解决方案是从 $(document) 中取消绑定所有事件名称,因为它看起来像是插件将所有事件附加到的位置。但即使这样也没有用。

jQuery(document).unbind("AddressChanged");
        jQuery(document).unbind("AutocompleteUsed");
        jQuery(document).unbind("VerificationInvoked");
        jQuery(document).unbind("RequestSubmitted");
        jQuery(document).unbind("ResponseReceived");
        jQuery(document).unbind("RequestTimedOut");
        jQuery(document).unbind("AddressWasValid");
        jQuery(document).unbind("AddressWasAmbiguous");
        jQuery(document).unbind("AddressWasInvalid");
        jQuery(document).unbind("AddressWasMissingSecondary");
        jQuery(document).unbind("OriginalInputSelected");
        jQuery(document).unbind("UsedSuggestedAddress");
        jQuery(document).unbind("InvalidAddressRejected");
        jQuery(document).unbind("AddressAccepted");
        jQuery(document).unbind("Completed");
4

2 回答 2

1

对于任何试图在 Angular 应用程序中实现 SmartyStreets jquery.liveaddress 插件的人来说都可以。

诀窍是不注册匿名事件处理程序。

在我的情况下,我在指令中实例化 SmartyStreets,因此将所有 eventHandlers 放在作用域上并在 $destroy 上调用 liveAddress.deactivate()。不再有重复的事件。

于 2016-01-29T00:06:54.457 回答
0

在 jQuery 中,事件处理函数存储在一个数组中。因此,unbind()函数只查找上述数组中的函数。这进一步意味着您只能unbind()使用已添加的事件处理程序bind()

查看.bind()上的 jQuery 文档

于 2016-01-27T23:42:40.667 回答