I am modifying an Angular template that is currently using innerHTML to dynamically build HTML fragments into the DOM. In this case, I am creating a mat-table
and dynamically building the header.
Here is the existing template code before my changes. This works as expected calling the buildLabelFromValues()
through the [innerHtml]
directive:
<div class="mat-table">
<!--HEADER-->
<div class="mat-header-row mat-option no-pointer optgroup-header table mat-primary"
[innerHtml]="'localStr.' + buildLabelFromValues(autoCompleteList[0], true) | stringLocalize | async">
</div>
...
</div>
However, I need to explicitly build the table header in a more "Angular" way (in order to prepare for setting click/change listeners on the header cells). I can't do this through elements created from the innerHtml directive. So, my modified template code is below:
<div class="mat-header-row mat-option no-pointer optgroup-header table mat-primary">
{{ this.buildHeaderLabelFromValues(autoCompleteList[0]) }}
</div>
The issue I have currently, is that with the modified template code above, the response from buildHeaderLabelFromValues()
is an htmlEncoded string of characters. I need the output to be in actual HTML.
Finally, here is the method that builds the header:
buildHeaderLabelFromValues(value: any): string {
console.log('value:', value);
let label = '';
forEach(value, (_value: any, _key: string) => {
if (!this.hiddenColumns.includes(_key)) {
if (_value === null) { _value = undefined; } // don't display "null"
label = `${label}<div title='${cleanHtmlTitle(_value)}' class="table-col mat-header-cell">${this.formatHeaderText(_key.toString())}</div>`;
}
});
return label;
}
I suppose the issue could be that because the method above is of type: string, the html carets "<" and ">" are being encoded as <
and >
as part of the interpolated values, something the [innerHTML] directive does not do. I just need to understand how to resolve it so that I can move on with the refactor of this template.