ng-include compiles to a comment

2019-04-18 07:34发布

I have an angular app, and want to in a template, add other as divs. ng-include seemed like a perfect choice.

In main.html

<div id="page-wrapper">
<div ng-include src="'/partials/module.html'"></div>
</div>

In module.html

<div class="module">
<h1>Module</h1>
</div>

All files live in the partials-folder in the app.

When running this, at the place in the html code, I get <!-- ngInclude: -->, and substituting the divs in main.html with the also supported ng-include tag, it compiles to <!-- ngInclude: undefined -->.

Does anyone know what might be the problem?

4条回答
手持菜刀,她持情操
2楼-- · 2019-04-18 07:40

I was facing the exact issue, compiler might not be finding the src path you provided. remove the first / in the path. compiler will automatically append / to the root context and tries to find the path. It worked for me.

<div ng-include src="'/partials/module.html'"></div> 

replace with

<div ng-include src="'partials/module.html'"></div>
查看更多
不美不萌又怎样
3楼-- · 2019-04-18 07:52

You only use src="" when using ng-include as an element:

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

When using ng-include as an attribute, you put the partial URL right in the attribute itself:

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

See https://docs.angularjs.org/api/ng/directive/ngInclude for the two use-cases.

查看更多
淡お忘
4楼-- · 2019-04-18 07:56

It must be a scope issue. Make sure the $scope object is passed properly and do not forget to include the controllers and services pertaining to the partial in your main page!

查看更多
爷、活的狠高调
5楼-- · 2019-04-18 07:58

After hours I resolved it for mine, it's all about using single quotes using between double quotes.

<!--it will not work-->
<ng-include src="views/header.html"></ng-include>

<!--it will work :)-->
<!--Difference is single quotes along with double quotes around src string-->
<ng-include src="'views/header.html'"></ng-include>
查看更多
登录 后发表回答