与angular.js中的hashchange事件和$ stateChangeStart混淆

时间:2016-12-07 06:23:53

标签: javascript angularjs angular-ui-router hashchange

    $rootScope.$on('$stateChangeStart', function(event, toState  , toParams, fromState, fromParams) 
    {

            if(toState.params.isRestricted === true && !$rootScope.isAppLog ) {
                event.preventDefault();
            }

            // here I want value returned by the detectBackOrForward() onhashchange that exwcutes on back or forward button click
    });

    var detectBackOrForward = function(onBack, onForward) {
      hashHistory = [window.location.hash];
      historyLength = window.history.length;

      return function() {
        var hash = window.location.hash, length = window.history.length;
        if (hashHistory.length && historyLength == length) {
          if (hashHistory[hashHistory.length - 2] == hash) {
            hashHistory = hashHistory.slice(0, -1);
            onBack();
          } else {
            hashHistory.push(hash);
            onForward();
          }
        } else {
          hashHistory.push(hash);
          historyLength = length;
        }
      }
    };

    window.addEventListener("hashchange", detectBackOrForward(
      function() { return  "back" },
      function() { return  "forward" }
    ));

在上面的代码中,当我分别点击浏览器向后转发按钮时,我成功返回了"back""forward"值。但是我无法在$stateChangeStart事件中使用返回的值。请帮我解决这个问题。

0 个答案:

没有答案