Introduction:
A simple demonstration of HTML structure:
<td ng-repeat="col in cols">
<div ng-bind-html="col.safeHTML"></div>
</td>
JavaScript controller code snippet:
$scope.cols = [
{
field : 'logo',
displayName : 'Logo',
cellTemplate: '<div style="color:red">{{col}}</div>'
},
{
field : 'color',
displayName : 'Color',
cellTemplate: '<div style="color:green">{{col}}</div>
}
];
Linking the directive with JavaScript using link function:
for (var i = 0, j = $scope.cols.length;
i < j;
i++) {
if ($scope.cols[i].hasOwnProperty('cellTemplate')) {
$scope.cols[i].safeHTML = $sce.trustAsHtml($scope.cols[i].cellTemplate);
}
}
Although the HTML is being properly escaped, the dynamic bindings like {{some_var}}
are not getting processed. How can we ensure Angular evaluates the bindings within the safe HTML content? I've attempted to utilize different binding methods such as ngBindTemplate
without success :(