AngularJS directive $destroy

Pwnna提出了一个问题:AngularJS directive $destroy,或许与您遇到的问题类似。

回答者Clark Pan给出了该问题的处理方式:

The i18n example you provided would work if you only ever used it once.

I don't think you should be doing the event binding inside the compile function. You can do it inside the link function instead:

------------------------- -------------- ---------------- -------------------- -------------- -
  ------ -
    --------- ------
    ----- --------------- -------- ------ -
      --- --------
      --- ------------ - ---------------
      ------------------------------------------------------- ---------------
      ------- - -------------------------------- ---------------- -
        ------------------------------------------------------- ------------ -- ---------
      ---
      --------------------- ---------- -
        -----------------------
        ----------
      ---
    -
  --
----

Alternatively, you could bind the event on the child scope itself, and use $broadcast on the $rootScope to trigger it. That way the event will automatically be garbage collected when the scope is destroyed:

------------------------- -------------- ---------------- -------------------- -------------- -
  ------ -
    --------- ------
    ----- --------------- -------- ------ -
      --- ------------ - ---------------
      ------------
      -------- ------------------
        ------------------------------------------------------- ------------ -- ---------
      -
      --------------------------- -----------
    -
  --
----

-------------------------------------- ---------

希望本文对你有帮助,欢迎支持JavaScript中文网

来源:JavaScript中文网 ,转载请注明来源 本文地址:https://www.javascriptcn.com/post/29180