ng包含不'ng应用程序获得名称时不起作用

ng-include doesn't work when ng-app gets a name

本文关键字:ng 不起作用 应用程序 包含不      更新时间:2023-09-26

我正在使用ng-include来包含导航条。与ng app="组合使用效果良好。但当我使用一个名称(例如,ng-app="myApp")时,它就不再起作用了。请参见示例。

<!DOCTYPE html>
<html>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <body >
        <div ng-app="">
        <div ng-include="'pages/navbar.html'"></div>
            <script src="app.js"</script>
        </div>
    </body>
</html>

app.js

var app = angular.module('myApp', []);
app.controller('mainController', function($scope) {
   });

上面的操作很好,但当我插入名称"myApp"时,它停止包括导航栏。

<!DOCTYPE html>
<html>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <body >
        <div ng-app="myApp">
            <div ng-include="'pages/navbar.html'"></div>
            <script src="app.js"</script>
        </div>
    </body>
</html>

为什么

您在这里有一个打字错误

<script src="app.js"</script>

应该是

<script src="app.js"></script>

只要它在angular.js之后加载,那么你的应用程序就应该能够找到myApp模块的声明(你已经要求它使用ng-app="myApp"

在浏览器控制台中检查是否有任何错误-如果没有加载脚本,您将看到类似cannot find module 的错误

我相信您已经错过了以正确的样式包含app.js文件。控制台里有错误吗?

已编辑

<!DOCTYPE html>
<html>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <body >
        <div ng-app="myApp">
            <div ng-include="'pages/navbar.html'"></div>
        </div>
<script src="app.js"></script>
    </body>
</html>