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

标签 javascript html angularjs syntax include

我试图在 ng-repeat 中包含一个 HTML 片段,但我无法让包含工作。似乎 ng-include 的当前语法与以前的不同:我看到很多示例使用

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

但在 official docs ,它说使用

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

然后down the page它显示为

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

我的代码片段不是很多,但有很多事情要做;我读过 Dynamically load template inside ng-repeat这可能会导致问题,因此我将 sidepanel.html 的内容替换为仅单词 foo,仍然没有。

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

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

并通过引用脚本的 id 的所有变体 ng-include,仍然没有。

我的页面包含更多内容,但现在我将其精简为以下内容:

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

最佳答案

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

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

Source

关于javascript - ng-include 的正确语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13943471/

上一篇:网页未找到

下一篇:html - 轮廓半径?

相关文章:

angularjs - 如何在单击标记时重定向到下一页并显示有关标记的详细信息?

javascript - Handlebars 模板和动态图像

javascript - 从 javascript 表(?)导入数据到 excel

javascript - 如何将 $scope 变量包含到自定义过滤器函数中

javascript - 在 Angular-JS 中获取数组大小

javascript - AngularJS - 检查类型为 "number"的输入是否有值,0 是有效值

javascript - ionic :unable to add image just above ionic tabs

javascript - Jquery 将下拉列表中的值添加到文本输入

php - 想要我的文本区域格式它是像 html 这样的只读文本

javascript - 窗口 onhashchange 不工作