AngularJS: Watch out for HTML comments in those templates!

One day, using


in your directive configuration might get you a :

Error: Template must have exactly one root element. was:<your root element>…

error whether you’re providing a template string or loading through templateUrl. I went back and double checked my HTML and it looks like I indeed had only a single root element in my template but lo and behold, that root element was followed by an HTML comment like:

<div>…</div><!– This is an html comment–>

From the point of view of Angular I indeed had sinned. Removing the comment fixed the issue. What to remember here is that first, don’t create sibling to your root element in your template files or strings, and second, make sure comments are within that root element or removed.

Published by Abou Kone

I am a front end architect with 10+ years of experience in web development. The best part of the process for me is converting ideas into code and solving the technical problems that come along. Alongside providing technical leadership and architectural support to projects spanning multiple industries, I am also experienced in leading discussions with designers, developers, and business stakeholders helping to guide teams in turning complex business workflows or data into easy-to-use web and mobile interfaces. I believe in delivering high quality products and am constantly looking into improving the process and tools use to achieve this goal.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: