I need to change the innerHTML of an element and I've decided to create an attribute directive to accomplish this task. I have created the following example to illustrate what I'm basically trying to do:
I have used interpolation to set the content of a paragraph:
<p changeContentDirective>{{content}}</p>
The 'changeContentDirective' modifies the innerHTML of the paragraph if the use hovers over it.
this.el.nativeElement.innerHTML = 'modified by directive';
I had also set a function to execute after 5s that will change the value of the 'content' property:
ngOnInit() {
setTimeout(() => {
console.log('timeout: modify content');
this.content = 'modifed after timeout'
}, 5000);
}
If I don't hover over the paragraph, the content is being changed after 5s. The strange part is that the content is not modified by the handler from setTimeout if I hover over the element (obviously before the handler gets executed)
I'm not sure why this is happening, it seems that after I modify the innerHTML programmatically, changing the 'content' property from the AppComponent doesn't have any effect on the html displayed by the paragraph.
In the actual application I'm working on, the structure is more complex, I don't have to modify the innerHTML of a paragraph, but the innerHTML of different td's that belong to a table (Kendo Grid), because of this I don't think I can use a pipe.
My question is whether I can modify the innerHTML of an element in a directive by accessing the native element and also why this strange issue occurs?
Thanks in advance