Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当页面上存在角度ng include时,HashChange事件停止触发_Javascript_Angularjs_Hashchange - Fatal编程技术网

Javascript 当页面上存在角度ng include时,HashChange事件停止触发

Javascript 当页面上存在角度ng include时,HashChange事件停止触发,javascript,angularjs,hashchange,Javascript,Angularjs,Hashchange,我有以下HTML+Angular应用程序外壳,可以完成我需要它完成的所有操作: 如果页面加载时URL哈希为空,则会将其初始化为/。 每当URL哈希更改时,视图变量都会更新。 这一点非常有效: <!DOCTYPE html> <html ng-app> <head> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

我有以下HTML+Angular应用程序外壳,可以完成我需要它完成的所有操作:

如果页面加载时URL哈希为空,则会将其初始化为/。 每当URL哈希更改时,视图变量都会更新。 这一点非常有效:

<!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事件停止触发。我是做错了什么还是这是一个错误?有什么解决办法吗?

有什么原因不使用$route服务吗?好问题。我试过了,但没能让它在这个用例中工作。我将接受一个答案,它实现了与我在这里所做的相同的功能,但使用了$route服务。具体来说,我希望路由处理程序是一个函数,而不是通过$routeProvider.when.same将散列映射到部分和控制器。。。ChristopherJamesCalo有什么解决办法吗?我从来没有找到一个解决办法,@caarlos0,但我在发布这个问题后也没怎么看。我不确定1.2是否解决了这个问题。