我寫了一個指令是這樣的: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>
,那麼爲什麼角度拋出這個錯誤?根元素的條件/定義是什麼?
此外,在您的鏈接功能的所有代碼不是必需的,這就是爲什麼你必須聲明範圍。 – TheHippo