我有以下HTML + Angular应用程序shell,它可以完成我需要做的所有事情:
#/
。view
变量都会更新。这非常有效:
<!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
事件将停止触发。我做错了什么或这是一个错误?任何解决方法?