foundation-apps icon indicating copy to clipboard operation
foundation-apps copied to clipboard

Template for directive 'zfOffcanvas' must have exactly one root element.

Open spsaucier opened this issue 9 years ago • 16 comments

Similarly to issue #377, I'm getting the following error: Error: [$compile:tplrt] Template for directive 'zfOffcanvas' must have exactly one root element. components/offcanvas/offcanvas.html

This is on a fresh installation of the newest version of Foundation for Apps. I haven't updated the default components/offcanvas/offcanvas.html, and the error persists even if I use only the following to call it: <zf-offcanvas position="left" id="menu"></zf-offcanvas>

I'd guess it has to do with the HTML to JS gulp process using ngHtml2js, but I'm not certain.

spsaucier avatar Mar 19 '15 19:03 spsaucier

Can you try using the directive as an attribute instead of an element?

gakimball avatar Mar 19 '15 20:03 gakimball

That error happens when the template specified for directive does not have a single root and the deprecated replace: true option is used.

On Thu, Mar 19, 2015 at 10:42 PM Geoff Kimball [email protected] wrote:

Can you try using the directive as an attribute instead of an element?

— Reply to this email directly or view it on GitHub https://github.com/zurb/foundation-apps/issues/542#issuecomment-83755197 .

circlingthesun avatar Mar 20 '15 11:03 circlingthesun

@gakimball yes, using <div zf-offcanvas position="left" id="menu"></div> shows the same error. @circlingthesun Right, that's where I'm guessing ngHtml2js is mis-formatting the built-in offcanvas.html, which is this: <div class="off-canvas {{ position }}" ng-class="{'is-active': active}" ng-transclude></div>

spsaucier avatar Mar 20 '15 12:03 spsaucier

Well, there is an quick way to find out. Inject $templateCache and then run console.log($templateCache.get('components/offcanvas/offcanvas.html'))

circlingthesun avatar Mar 20 '15 12:03 circlingthesun

@circlingthesun Inject into the run() function? function run($templateCache) { FastClick.attach(document.body); console.log($templateCache.get('components/offcanvas/offcanvas.html')); } This results in Uncaught Error: [$injector:unpr] Unknown provider: tProvider <- t, which also seems to be a product of minification - I don't know of an alternate format for injecting ng variables in functions. Trying to inject $templateCache into controllers or config.$inject & use it results in ReferenceError: $templateCache is not defined

Side note: would something like ng-annotate in the build process improve this situation?

spsaucier avatar Mar 20 '15 13:03 spsaucier

Yeah, ng-nnotate would fix it. But you could also do what is suggested under the 'A Note on Minification' section at https://docs.angularjs.org/tutorial/step_05

circlingthesun avatar Mar 20 '15 13:03 circlingthesun

Hmm, well I did that here: config.$inject = ['$urlRouterProvider', '$locationProvider', '$templateCache']; function config($urlProvider, $locationProvider, $templateCache) { console.log($templateCache.get('components/offcanvas/offcanvas.html')); ... });

and got Unknown provider: $templateCache. Is $templateCache built-in or do I need to add a module?

spsaucier avatar Mar 20 '15 13:03 spsaucier

You can only inject providers in the config function. Inject it to a controller or run function.

circlingthesun avatar Mar 20 '15 13:03 circlingthesun

Ah, I see. Well, that console.log results in undefined.

spsaucier avatar Mar 20 '15 13:03 spsaucier

This error is still persisting in the most up-to-date version of Foundation for Apps. The undefined console.log message means that the built-in template for zfOffcanvas is not being loaded into the app correctly, right?

spsaucier avatar Apr 28 '15 13:04 spsaucier

This might not be related, but there aren't many foundation-app specific resources on this topic.

I was having a similar problem with all the components, turns out I was missing a link to the compiled templates.js file.

<script src="/assets/js/templates.js"></script>

leptest avatar May 13 '15 10:05 leptest

@spsaucier Are you still getting this issue after including the templates.js file in your index.html file?

jamieshark avatar Jul 13 '15 21:07 jamieshark

Including the templates.js file in index.html got rid of the error for me.

falldowngoboone avatar Aug 11 '15 19:08 falldowngoboone

+1 Template for directive 'zfPanel' must have exactly one root element.

I didn't compile the templates.js file because I bootstrapped the app and compiled with Grunt, but even including the foundation-apps-templates.js and foundation-apps.js didn't fix anything.

mattfili avatar Aug 20 '15 02:08 mattfili

If you encounter this problem, make sure you have a correct order of foundation scripts on your page:

<script type="text/javascript" src="path/to/foundations/apps/dist/js/foundation-apps.js"></script>
<script type="text/javascript" src="path/to/foundations/apps/js/angular/foundation.js"></script>
<script type="text/javascript" src="path/to/foundations/apps/js/angular/foundation-apps-templates.js"></script>

fortuneteller avatar Feb 11 '16 12:02 fortuneteller

inside your directive's templateUrl you should use <div></div> instead of something like this

<p>...</p>
<div>...</div>
<div</div>

just this... :)

pilapila avatar Jul 14 '17 07:07 pilapila