2013-05-21 53 views
6

我寫了一個指令是這樣的:Angular的根元素是什麼?

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; 

     } 
    }; 
}); 

我用它是這樣的:

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

我想的是,<headersort sortBy="Name" title="Product">被替換<th>Product</th>。但是,我得到一個錯誤說:

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

但我有一個根元素,對不對?我的根元素是<th>,那麼爲什麼角度拋出這個錯誤?根元素的條件/定義是什麼?

+0

此外,在您的鏈接功能的所有代碼不是必需的,這就是爲什麼你必須聲明範圍。 – TheHippo

回答

5

看看this問題。

嘗試從restrict: 'E'改變你的指令來restrict: 'A'和改變你的HTML <th headersort sortBy="Name" title="Product"></th>

+0

謝謝。問題的鏈接確實是我的問題。我現在解決它作爲一個屬性,而不是表頭。 – Martijn

0

我認爲它對table元素的說法。你有沒有定義它?