ng-include compiles to a comment

◇◆丶佛笑我妖孽 提交于 2019-12-04 22:44:30

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.

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>

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>

I got stuck in the same issue, none of the solutions mentioned above helped me out to solve the problem and it ate my two whole days. Finally I figured out with the help of this link.

Also the solution to make this work requires below two traits:

  1. partial html should be included in single quotes
  2. secondly full path of the file needs to be mentioned even if the file is present in the same location.

eg:

ABC
|
---- DEF
      |
      --- abc.html
      --- partial.html

So if abc.html has ng-include syntax then it should be like as below

<div ng-include="'ABC/DEF/partial.html'"></div>
                     OR
<ng-include src="'ABC/DEF/partial.html'"></ng-include>
sangeetha k p

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!