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
AngularJS ng-include doesn't work
提问by Haradzieniec
Here is a source of the main.html
file
这是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.html
that contains a plain text 'part'
- just several symbols.
还有一个part.html
包含纯文本'part'
- 只有几个符号。
Both files main.html
and part.html
are located in the same folder.
两个文件main.html
和part.html
都位于同一文件夹中。
When I open main.html
in the browser, I don't see three repeats of the content from part.html
however I see three Hello:
. Seems like part.html
is 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
回答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?
回答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 文件一样运行。