AngularJS ng-include不起作用

问题描述 投票:1回答:3

这是main.html文件的来源

<!DOCTYPE html>
<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

    <script>
        var myApp = angular.module ('myApp',[]);
        myApp.controller('myCtrl', function ($scope){
            $scope.name = "Tim";
            $scope.partialStuff = [ 'a', 'b', 'c' ];
            alert(window.angular.version.full);//1.3.14 is displayed
        });
    </script>
</head>
<body ng-app="myApp">
    <div ng-controller="myCtrl">
            {{name}}
        <div ng-repeat="partialVar in partialStuff">
            Hello: <div ng-include src="'part.html'"></div>
        </div>
    </div>
</body>
</html>

还有一个part.html包含一个纯文本'part' - 只是几个符号。

文件main.htmlpart.html都位于同一个文件夹中。

当我在浏览器中打开main.html时,我看不到来自part.html的内容的三个重复但是我看到三个Hello:。好像part.html似乎没有加载。

为什么?

谢谢。

请将其放在body标签内以使其正常工作

<script type="text/ng-template" id="part.html">
  part
</script>
javascript html angularjs angularjs-ng-repeat angular-directive
3个回答
5
投票

您无法使用file:// protocol直接导入本地文件。

你应该看看:Is it possible to use ng-include without web server?


14
投票
<div ng-include="'part.html'"></div>

我应该是正确的语法。更多信息here

编辑:另外,文件路径应该来自根目录而不是html文件的相对路径。


0
投票

ng-include可用作:

<div>
    <ng-include src="demo.html"></ng-include>

这肯定会包含你的文件,就像普通的src一样。 ng-include也在本地服务器上工作。没有必要像tomcat那样添加额外的服务器来运行这个文件。这将像html文件一样运行。

© www.soinside.com 2019 - 2024. All rights reserved.