非常简单(代码如下)。我有两个 html 页面,test.htm 和 ang.htm。我从 test.htm 开始,通过一个指向 ang.htm 页面的超链接。 ang.htm 包含 Angular js,它有一个简单的 Controller 来执行 Hello World。我单击超链接导航至 ang.htm。但是,我需要刷新 ang.htm 页面才能运行 Angular 代码。这是为什么?
test.htm
<html>
<body>
<a href="http://localhost/ang.htm">Click</a>
</body>
</html>
ang.htm
<html>
<body ng-app="testApp" ng-controller="testController">
{{hello}}
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js"></script>
<script>
angular.module("testApp", []);
angular.module( "testApp" ).controller( "testController", function( $scope )
{
$scope.hello = "Hello world";
});
</script>
</body>
</html>
最佳答案
我只是根据文件结构将localhost/ang.htm更改为/ang.htm。它按预期工作。
使用 Firebug 进行调试对于您的情况非常有帮助。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<a href="/ang.htm">Click</a>
</body>
</html>
关于javascript - 除非刷新,AngularJS 不会运行 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27408022/