I have created a custom directive to out put a fairly simple tabular data. This is my current code:
app.directive('searchResult', function () {
return {
restrict: 'E',
scope: { content: '=' },
link: function (scope, element, attrs) {
var html = '<table class="table table-bordered">';
angular.forEach(scope.rows, function (row, index) {
if (row.names.length > 1) {
var names = row.names;
html += '<tr>';
html += '<td rowspan='+row.names.length+'>'+row.num +'</td>';
html += '<td>'+names[0] + '</td>';
html += '</tr>'
for (var i = 1; i < names.length; i++) {
html += '<tr>';
html += '<td>'+names[i] +'</td>';
html += '</tr>';
}
} else {
html += '<tr>';
html += '<td>'+row.num +'</td>';
html += '<td>'+row.names[0] + '</td>';
html += '</tr>';
}
})
html += '</table>';
element.replaceWith(html)
}
}
});
The problem with this code is the binding. If a user searches a new item, the directive will create a new table above the previously displayed result instead of replacing the old result.
I am trying to use templateUrl and have a template that has {{content}} inside and the binding works. The old search result is replaced by the new one.
Is there a way to perform this table code creation inside a template?