I have a table where the rows are repeated via As Pavlo wrote, you can move the Directive mytemplate.html plunker As pointed out in comments the template of a directive should have single root element. So I would suggest you to move the ng-repeat
.
I am trying to create a template that generates columns for each row <
tr
element to the template for the directive. Another option is to use a td
element and directive that replaces your td
with the template that you want to use.<table>
<tr ng-repeat="p in positions">
<td replace-me template="mytemplate.html" position="p"></td>
</tr>
</table>
replaceMe
.directive("replaceMe", ["$compile", '$http', '$templateCache', function ($compile, $http, $templateCache) {
return {
restrict: 'A',
scope: {
position: "="
},
link: function (scope, element, attrs) {
function getTemplate(template) {
$http.get(template, {cache: $templateCache}).success(function (templateContent) {
element.replaceWith($compile(templateContent)(scope));
});
}
scope.$watch(attrs.template, function () {
if (attrs.template) {
getTemplate(attrs.template);
}
});
}
}
}]);
<td>{{position.Name}}</td>
<td>{{position.Code}}</td>
<td another-my-directive></td>
tr
element to the template of the directive, like this: http://plnkr.co/edit/YjLEDSGVipuKTqC2i4Ng?p=preview