Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a root element in Angular?

I have written a directive like this:

app.directive('headersort', function () {
    return {
        restrict: 'E',
        scope: {
            sortBy: '=',
            title: '='
        },
        template: '<th>{{ title }}</th>',
        replace: true,
        link: function (scope, element, attributes) { 
            scope.sortBy = attributes.sortBy;
            scope.title = attributes.title;

        }
    };
});

And I use it like this:

<headersort sortBy="Name" title="Product">

What I want is that <headersort sortBy="Name" title="Product"> is replaced with <th>Product</th>. But I get an error saying:

Template must have exactly one root element. was: <th>{{ title }}</th>

But I do have one root element, right? My root element is <th>, so why is angular throwing this error? What are the conditions/definition of a root element?

like image 759
Martijn Avatar asked May 21 '13 09:05

Martijn


1 Answers

Take a look at this issue.

Try changing your directive from restrict: 'E' to restrict: 'A' and change your HTML to <th headersort sortBy="Name" title="Product"></th>

like image 93
rtcherry Avatar answered Sep 28 '22 08:09

rtcherry