I currently am making an app in Ionic 2/Angular 2, and have come across a hurdle I can't quite solve.
To give some insight, I'm getting a bunch of strings from a webservice, where some of the strings could look like this example:
"This is a string with a link: http://www.somelink.com"
I have set up a RegEx to find the link in the strings (if there are any) and convert them to an tag, as follows:
"This is a string with a link:
<a href="the link from above">the link from above</a>"
I want to put this string with the tag into following html:
<ion-item-sliding *ngFor="let item of wishes">
<div ion-item text-wrap style="user-select: text">
{{item.wish}}
</div>
</ion-item-sliding>
item.wish contains the "this is a string....." mentioned above, and I want it to actually create the link so it's clickable, but for some reason it displays the tag, instead of making it a link. I was expecting the interpolation to parse the HTML tag into a working link instead of just printing the entire tag.
UPDATE
In my ts file I have this:
urlify(text) {
var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
return text.replace(urlRegex, function (url) {
return "<a href=\"" + url + "\">" + url + "</a>";
})
}
A basic method for making this:
"This is a string with a link: http://www.somelink.com"
into this:
"This is a string with a link:
<a href="http://www.somelink.com">http://www.somelink.com</a>"
That string is then passed onto the HTML page, via the interpolation, shown above.
So the tag is created in the code, because it has to be dynamic creation of list items, so i cannot set the tag in the HTML. If I created the tag in the HTML like:
<a [href]="{{item.wish}}">somelink</a>
It would also make the text Next to the link ("This is a string with a link") into a link as well, which is not the intention, only the link should be clickable.