什么是ng-include的正确语法?

我试图在一个ng-repeat包含一个HTML代码片段,但是我无法让这个include工作。 看来ng-include的当前语法与之前的不同:我看到很多使用的例子

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

但在官方文件中 ,它说使用

 <div ng-include="path/file.html"></div> 

但是,然后下面的页面显示为

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

无论如何,我尝试过

 <div ng-include="views/sidepanel.html"></div> 
 <div ng-include src="views/sidepanel.html"></div> 
 <ng-include src="views/sidepanel.html"></ng-include> 
 <ng-include="views/sidepanel.html"></ng-include> 
 <ng:include src="views/sidepanel.html"></ng:include> 

我的代码片段不是很多代码,但有很多事情要做。 我读dynamic加载模板内ng-repeat ,这可能会导致一个问题,所以我用fooreplacesidepanel.html的内容,仍然没有。

我也尝试直接在页面中声明模板,如下所示:

 <script type="text/ng-template" id="tmpl"> foo </script> 

并且运行ng-include所有变体引用脚本的id ,但是什么都没有。

我的网页有更多的,但现在我已经把它剥离到这一点:

 <!-- index.html --> <html> <head> <!-- angular includes --> </head> <body ng-view="views/main.html"> <!-- view is actually set in the router --> <!-- views/main.html --> <header> <h2>Blah</h2> </header> <article id="sidepanel"> <section class="panel"> <!-- will have ng-repeat="panel in panels" --> <div ng-include src="views/sidepanel.html"></div> </section> </article> <!-- index.html --> </body> </html> 

标题呈现,但我的模板不。 我在控制台或Node中没有发现任何错误,如果在开发工具中单击src="views/sidepanel.html"中的链接,它会将我带到我的模板(并显示foo )。

你必须在双引号内引用你的srcstring:

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

资源

  <ng-include src="'views/sidepanel.html'"></ng-include> 

要么

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

要么

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

记住要点:

– >在src中没有空格

– >记得在src的双引号中使用单引号

对于这些故障排除,重要的是要知道ng-include要求urlpath来自app根目录,而不是来自partial.html所在的目录。 (而partial.html是可以find内联ng-include标记标签的视图文件)。

例如:

正确:div ng-include src =“'/views/partials/tabSlides/add-more.html'”>

不正确:div ng-include src =“'add-more.html'”>

对于那些正在寻找最短的“项目渲染器”解决scheme的人来说, ng-repeat和ng-combo的组合包括

<div ng-repeat="item in items" ng-include src="'views/partials/item.html'" />

实际上,如果你这样用一个中继器,它会起作用,但是不会为其中的两个! Angular(v1.2.16)会因为某些原因而被吓倒,如果你有两个这样一个接一个的话,所以closuresdiv的方式比pre-xhtml更安全:

<div ng-repeat="item in items" ng-include src="'views/partials/item.html'"></div>

也许这对初学者有帮助

 <!doctype html> <html lang="en" ng-app> <head> <meta charset="utf-8"> <title></title> <link rel="icon" href="favicon.ico"> <link rel="stylesheet" href="custom.css"> </head> <body> <div ng-include src="'view/01.html'"></div> <div ng-include src="'view/02.html'"></div> <script src="angular.min.js"></script> </body> </html> 

这对我工作:

 ng-include src="'views/templates/drivingskills.html'" 

完整的div:

 <div id="drivivgskills" ng-controller="DrivingSkillsCtrl" ng-view ng-include src="'views/templates/drivingskills.html'" ></div> 

尝试这个

 <div ng-app="myApp" ng-controller="customersCtrl"> <div ng-include="'myTable.htm'"></div> </div> <script> var app = angular.module('myApp', []); app.controller('customersCtrl', function($scope, $http) { $http.get("customers.php").then(function (response) { $scope.names = response.data.records; }); }); </script>