In my Angular2 component, I receive a string from the parent component, that contains some HTML and it is being injected into the view. This part works! I now want to highlight a number of first words in that html (note: that number changes every time, let's say 15 for now).
Code:
1) html string (content) contains somethink like this:
<p>Hello, this is a <span> smaple text</span></p>
<p>More things to say here...</p>
<p>Perhaps an image as well here, or a link: <a> This is a link here</a></p>
<p>This is very long, did you read this?</p>
2) In Angular2 this string is injected into the component:
<div [innerHTML]="content"> </div>
<button type="submit" (click)="toggle()" >Toggle Highlight</button>
3) When toggle btn is clicked: the plan is to count 15(or whats specified) words in the html string, and inject a span(with green background) from start to word #15..
Right now, I am currently trying to simply inject a span with a green background colour around the whole html, like this:
toggle() {
this.content = "<span style=\"background-color: green;\">"+ this.content +"</span>";
}
Even this is not working, I also tried with a class, but it's not being picked up. And there is another problem with inserting a span into this html as word 15 may be in a different tag which will cause problems.
Any suggestion if there is another way to highlight a number of words in string of html?