更改状态AngularJS时删除函数

时间:2016-01-22 16:39:41

标签: javascript angularjs angular-ui-router

我有一个函数可以执行一些dom操作,这会对网站产生很好的影响,但我只希望这个函数应用于某些视图,而不是所有视图。

现在,当我访问此状态时,动画正常运行但在我浏览其他状态时继续运行。

这是功能:

'use strict'

angular.module('myApp')
  .controller('ExampleCtrl', function ($scope) {

    (function () {

      // detect if IE : from http://stackoverflow.com/a/16657946        
      var ie = (function () {
        var undef, rv = -1; // Return value assumes failure.
        var ua = window.navigator.userAgent;
        var msie = ua.indexOf('MSIE ');
        var trident = ua.indexOf('Trident/');

        if (msie > 0) {
          // IE 10 or older => return version number
          rv = parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
        } else if (trident > 0) {
          // IE 11 (or newer) => return version number
          var rvNum = ua.indexOf('rv:');
          rv = parseInt(ua.substring(rvNum + 3, ua.indexOf('.', rvNum)), 10);
        }

        return ((rv > -1) ? rv : undef);
      }());


      // disable/enable scroll (mousewheel and keys) from http://stackoverflow.com/a/4770179                    
      // left: 37, up: 38, right: 39, down: 40,
      // spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: 36
      var keys = [32, 37, 38, 39, 40],
        wheelIter = 0;

      function preventDefault(e) {
        e = e || window.event;
        if (e.preventDefault)
          e.preventDefault();
        e.returnValue = false;
      }

      function keydown(e) {
        for (var i = keys.length; i--;) {
          if (e.keyCode === keys[i]) {
            preventDefault(e);
            return;
          }
        }
      }

      function touchmove(e) {
        preventDefault(e);
      }

      function wheel(e) {
        // for IE 
        //if( ie ) {
        //preventDefault(e);
        //}
      }

      function disable_scroll() {
        window.onmousewheel = document.onmousewheel = wheel;
        document.onkeydown = keydown;
        document.body.ontouchmove = touchmove;
      }

      function enable_scroll() {
        window.onmousewheel = document.onmousewheel = document.onkeydown = document.body.ontouchmove = null;
      }

      var docElem = window.document.documentElement,
        scrollVal,
        isRevealed,
        noscroll,
        isAnimating,
        container = document.getElementById('container'),
        trigger = container.querySelector('button.trigger');

      function scrollY() {
        return window.pageYOffset || docElem.scrollTop;
      }

      function scrollPage() {
        scrollVal = scrollY();

        if (noscroll && !ie) {
          if (scrollVal < 0) return false;
          // keep it that way
          window.scrollTo(0, 0);
        }

        if (classie.has(container, 'notrans')) {
          classie.remove(container, 'notrans');
          return false;
        }

        if (isAnimating) {
          return false;
        }

        if (scrollVal <= 0 && isRevealed) {
          toggle(0);
        } else if (scrollVal > 0 && !isRevealed) {
          toggle(1);
        }
      }

      function toggle(reveal) {
        isAnimating = true;

        if (reveal) {
          classie.add(container, 'modify');
        } else {
          noscroll = true;
          disable_scroll();
          classie.remove(container, 'modify');
        }

        // simulating the end of the transition:
        setTimeout(function () {
          isRevealed = !isRevealed;
          isAnimating = false;
          if (reveal) {
            noscroll = false;
            enable_scroll();
          }
        }, 1000);
      }

      // refreshing the page...
      var pageScroll = scrollY();
      noscroll = pageScroll === 0;

      disable_scroll();

      if (pageScroll) {
        isRevealed = true;
        classie.add(container, 'notrans');
        classie.add(container, 'modify');
      }

      window.addEventListener('scroll', scrollPage);
      trigger.addEventListener('click', function () {
        toggle('reveal');
      });
    })();
  });

每当我访问这个状态时都会执行此操作,但是在我浏览之后也会执行,并且每次我进入相同的状态时它都会重复,这会打破效果。

如何以角度修复此问题?

谢谢!

1 个答案:

答案 0 :(得分:0)

找到一个可能的解决方案是在函数末尾添加它:

$scope.$on("$destroy", function () {
  window.removeEventListener('scroll', scrollPage);
});