Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

javascript - ng-include的正确语法是什么?(What is the correct syntax of ng-include?)

I'm trying to include an HTML snippet inside of an ng-repeat , but I can't get the include to work.(我正在尝试在ng-repeat包含一个HTML代码段,但我无法让include工作。)

It seems the current syntax of ng-include is different than what it was previously: I see many examples using(似乎ng-include的当前语法与以前的语法不同:我看到许多例子使用) <div ng-include src="path/file.html"></div> But in the official docs , it says to use(但在官方文档中 ,它表示要使用) <div ng-include="path/file.html"></div> But then down the page it is shown as(但是然后在页面上显示为) <div ng-include src="path/file.html"></div> Regardless, I tried(无论如何,我试过了) <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> My snippet is not very much code, but it's got a lot going on;(我的代码片段不是很多代码,但是它有很多内容;) I read in Dynamically load template inside ng-repeat that that could cause a problem, so I replaced the content of sidepanel.html with just the word foo , and still nothing.(我在读动态加载模板中ng-repeat这可能会导致一个问题,所以我代替的内容sidepanel.html只字foo ,并仍然一无所获。) I also tried declaring the template directly in the page like this:(我也尝试直接在页面中声明模板,如下所示:) <script type="text/ng-template" id="tmpl"> foo </script> And running through all the variations of ng-include referencing the script's id , and still nothing.(并贯穿ng-include所有变体,引用脚本的id ,但仍然没有。) My page had a lot more in it, but now I've stripped it down to just this:(我的页面中有更多内容,但现在我已将其删除到这样:) <!-- 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> The header renders, but then my template doesn't.(标题呈现,但我的模板没有。) I get no errors in the console or from Node, and if I click the link in src="views/sidepanel.html" in dev tools, it takes me to my template (and displays foo ).(我在控制台或Node中没有错误,如果我在开发工具中单击src="views/sidepanel.html"中的链接,它会将我带到我的模板(并显示foo )。)   ask by jacob translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to single quote your src string inside of the double quotes:(你必须在双引号内单引号你的src字符串:)

<div ng-include src="'views/sidepanel.html'"></div> Source(资源)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

1.4m articles

1.4m replys

5 comments

57.0k users

...