什么是正确的语法NG-包括哪些内容?(What is the correct syntax of n

2019-07-05 18:24发布

我想包括内部HTML片段ng-repeat ,但我可以得到包括不工作。 看来目前的语法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>

我的片段不是很多代码,但它有很多事情; 我在读动态加载模板中ng-repeat这可能会导致一个问题,所以我代替的内容sidepanel.html只字foo ,并仍然一无所获。

我也试过直接在这样的页面声明模板:

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

标题渲染,但后来我的模板没有。 我得到在控制台或节点没有错误,如果我点击链接src="views/sidepanel.html"的开发工具,它带我到我的模板(并显示foo )。

Answer 1:

你必须单引号您的src双引号内的字符串:

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

资源



Answer 2:

    <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双引号使用单引号



Answer 3:

对于故障排除,重要的是要知道,NG-包括需要的URL路径是从应用程序的根目录,而不是从同一目录partial.html生活在那里。 (而partial.html是视图文件内联NG-包括标记标签可以找到)。

例如:

正确的:DIV NG-包括SRC = “ '/views/partials/tabSlides/add-more.html'”>

不正确:DIV NG-包括SRC = “ '添加-more.html'”>



Answer 4:

对于那些谁正在寻找最短的“项目渲染器”从局部解决方案,所以NG重复和组合NG-包括

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

其实,如果你使用这样的一个中继器,它会工作,但不会为他们2的! 角(v1.2.16)会发疯出于某种原因,如果你有这一个接一个的2,所以是比较安全的关闭div中的XHTML预方式:

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



Answer 5:

也许这将帮助初学者

<!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>


Answer 6:

这为我工作:

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

完整的div:

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


Answer 7:

试试这个

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


Answer 8:

在NG-建设,未找到文件发生(404)错误。 因此,我们可以使用下面的代码

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

insted的的,

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


文章来源: What is the correct syntax of ng-include?