当页面上存在Angular ng-include时,HashChange事件将停止触发

时间:2012-12-04 22:19:54

标签: javascript angularjs hashchange

我有以下HTML + Angular应用程序shell,它可以完成我需要做的所有事情:

  1. 如果网页加载时网址哈希为空,则会初始化为#/
  2. 每当URL哈希发生变化时,view变量都会更新。
  3. 这非常有效:

    <!DOCTYPE html>
    <html ng-app>
      <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.js"></script>
        <script>
          function Router($scope) {
            if (location.hash === "") location.hash = "#/";
            $scope.view = createView(location);
            window.addEventListener("hashchange", function (event) {
              $scope.view = createView(event.newURL);
              $scope.$apply();
            });
          }
          function createView(url) {
            var parser = document.createElement("a");
            parser.href = url.toString();
            var hash = parser.hash;
            return (hash.replace("#/", "/views/") + "/index.html").replace("//", "/");
          }
        </script>
      </head>
      <body ng-controller="Router">
        Navigation:
        <a href="#/one/">one</a>
        <a href="#/two/">two</a>
        Partial: {{view}}
        <!--<div ng-include="view"></div>-->
      </body>
    </html>
    

    然后问题是,只要我取消注释div[ng-include],一切就会停止工作。具体来说,hashchange事件将停止触发。我做错了什么或这是一个错误?任何解决方法?

0 个答案:

没有答案