猿问

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

我正在尝试在内添加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>

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


慕森卡
浏览 674回答 3
3回答

梵蒂冈之花

您必须src在双引号内将字符串单引号引起来:<div ng-include src="'views/sidepanel.html'"></div>

白猪掌柜的

&nbsp; &nbsp;<ng-include src="'views/sidepanel.html'"></ng-include>要么&nbsp; &nbsp; <div ng-include="'views/sidepanel.html'"></div>要么&nbsp; &nbsp; <div ng-include src="'views/sidepanel.html'"></div>要记住的要点:-> src中没有空格->记住对src使用双引号

ITMISS

对于解决这些问题,重要的是要知道ng-include要求url路径来自应用程序的根目录,而不是来自partial.html所在的目录。(而partial.html是可以找到内联ng-include标记标签的视图文件)。例如:正确:div ng-include src =“'/views/partials/tabSlides/add-more.html'”>错误:div ng-include src =“'add-more.html'”>
随时随地看视频慕课网APP
我要回答