javascript AngularJS ng-include 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29520883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:40:10  来源:igfitidea点击:

AngularJS ng-include doesn't work

javascripthtmlangularjsangularjs-ng-repeatangular-directive

提问by Haradzieniec

Here is a source of the main.htmlfile

这是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>

Also there is a part.htmlthat contains a plain text 'part'- just several symbols.

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

Both files main.htmland part.htmlare located in the same folder.

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

When I open main.htmlin the browser, I don't see three repeats of the content from part.htmlhowever I see three Hello:. Seems like part.htmlis not loaded.

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

Why?

为什么?

Thank you.

谢谢你。

Please place it within the body tag to make it work

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

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

回答by Alessandro Cifani

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

should be the correct syntax I believe. More info here

应该是我相信的正确语法。更多信息在这里

EDIT: also, the file path should be from the root and not relative of the htmlfile.

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

回答by aleger

You cannot import local files directly using the file:// protocol.

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

You should take a look at: Is it possible to use ng-include without web server?

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

回答by Manish Bansal

ng-include can be used as:

ng-include 可以用作:

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

This will surely include your files like a normal src. ng-include work on local server also.There is no need to put extra server like tomcat to run this file. This will run simply like a html file.

这肯定会像普通 src 一样包含您的文件。ng-include 也可以在本地服务器上工作。不需要像 tomcat 这样的额外服务器来运行这个文件。这将像 html 文件一样运行。