简体   繁体   English

如何正确使用ng-include

[英]How the right way to use ng-include

I'm beginner on Angular.js and am having some problems with the directive ng-include . 我是Angular.js的初学者,并且在指令ng-include方面遇到了一些问题。

I want to display contents of header.html inside index.html 我想在index.html显示header.html内容

Well, here's my tree example: 好吧,这是我的树例子:

myapp
.    public
.    .     lib
.    .     .  angular.js
.    .     .  
.    header.html
.    index.html

Header.html code: Header.html代码:

<h1>Hello Angular</h1>

Index.html code: Index.html代码:

<html ng-app>
  <head> 
    <meta charset="utf-8">
    <script src="public/lib/angular.js"></script> 
  </head> 
  <body>  
    <ng-include src="'header.html'"></ng-include> 
  </body>   
</html>

When i run index.html , i have something like this: 当我运行index.html ,我有这样的事情:

<!-- ngInclude: undefined -->

I'm not sure if there's an ng-include tag to include other templates. 我不确定是否有包含其他模板的ng-include标签。 We usually do it this way 我们通常这样做

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

Are you accessing the app through a web server or by accessing the file through file:// protocol? 您是通过Web服务器访问应用程序还是通过file:// protocol访问该文件? In the latter case it won't work due to browser's security restrictions. 在后一种情况下,由于浏览器的安全限制,它将无法工作。 The solution would be to install Apache or Nginx and access the app through something like http://localhost/myapp/index.html . 解决方案是安装Apache或Nginx并通过http://localhost/myapp/index.html等方式访问应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM