AngularJS ng include dons'不起作用

AngularJS ng-include doesn't work

本文关键字:不起作用 dons ng include AngularJS      更新时间:2023-09-26

以下是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

为什么?

谢谢。

请将其放置在正文标签中以使其工作

<script type="text/ng-template" id="part.html">
  part
</script>
<div ng-include="'part.html'"></div>

应该是我认为正确的语法。更多信息点击这里

EDIT:此外,文件路径应该来自根,而不是相对于html文件。

不能使用file://协议直接导入本地文件。

你应该看看:是否可以在没有web服务器的情况下使用ng include?

ng-include可用作:

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

这肯定会像普通src一样包含您的文件。ng还包括在本地服务器上的工作。不需要像tomcat那样放置额外的服务器来运行这个文件。这将像一个html文件一样简单地运行。