Template for directive 'mydirective' must have exactly one root element
I noticed that by having a comment at the top level of my directive template causes the error:
Template for directive 'mydirective' must have exactly one root element
Here's my directive code:
angular.module('web').directive('mydirective', function() {
return {
restrict: 'A',
replace: true,
transclude: true,
scope: {
user: "=user"
},
templateUrl: 'directive/section.html',
link: function(scope, element, attrs, fn) {
}
};
});
directive/section.html
<!-- Load the view for column 0 row 0 --> <-- THIS COMMENT CAUSES THE ERROR. IF I REMOVE IT I GET NO ERROR
<div ng-repeat="view in patient.views">
<div ng-if="view.row === 0 && view.column === 0">
asdf
<div ng-include src="'templates/' + view.template + '.html'" ></div>
</div>
</div>
I'm currently using angular 1.2.16. Not sure if this has been addressed in later versions of angular.
@toymachiner62 since AngularJS is working with "live DOM" those comments are creating comment nodes in the DOM tree and thus you are ending up with 2 root nodes in your template (comment node and top-level div).
Now, not sure if AngualrJS could be "smarter" here - or rather - does it make sense to add additional logic to AngularJS to handle this case. One approach would be to remove all the root-level comment nodes but yeh, not sure if this case is worth adding additional processing in the framework....
In my opinion it should be smarter and handle comments if it's possible. Also what's the reason that you have to have only one root node?
dupe of #9212
Hi All, I have implemented UIbtimePicker Directive and it works fine with the default template. But if I try custom template I am getting this error
Template for directive 'uibTimepicker' must have exactly one root element. chargeEnquiry/UibTimePicker
- To keep it simple, In my custom template my code is as simple as
<div>Sample code</div>. - From the network logs I can find my custom template file loading up successfully which means there is no path error too.
- I have added
ui.bootstrapin myapp.moduleas well
What could have missed still to cause the above-mentioned error?