javascript - 除非刷新,AngularJS 不会运行 Controller

标签 javascript angularjs

非常简单(代码如下)。我有两个 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/

相关文章:

javascript - Array.isArray 和 jQuery.isArray 有什么区别?

javascript - CSS动态调整div重叠

javascript - 当没有表单名称时如何清除AngularJS中的表单

javascript - Angular JS 使用 $timeout 或 $interval 服务在后台获取信息

asp.net - 获取具有相同类的文本框的值数组

javascript - D3 圆圈包中单独的鼠标单击圆圈和背景

javascript - 如何使用云功能在Firebase中获取实时数据库?

javascript - 如何更改 AngularJS get 请求的结构?

javascript - 将时间戳转换为 Angular 表达式内的日期,如 {{new Date(timestamp)}}

Angularjs如何在 Controller 中获得常量