My api returns an array of events (json) with the following property:
event.text
The text property contains a string based on user input, for example companyName, I want to list the text in a ng-repeat loop with a clickable link to the company. (the event.text could be "Dave added a new company Philips" where Philips and Dave are values from a database)
Because the companyname can have html tags that are unsafe e.g.
<a href="somenastyurl">dangerouscompany</a>
angular sanitizes the strings.
However some links should be shown in the front-end, to make sure only save links (created by my api) are shown. I decided to add two return values in the api:
event.links
event.linknames
event.links contains links (urls to an item, the urls are always safe and not based on user input they use id's) and event.linknames the names that should be shown in the link. for example:
event.links = ["http://example.com/1","http://example.com/2"]
event.linknames = ["Dave","Philips"]
Ive edited event.text to contain tags like [0] to be replaced with links, a tag [0] should be replaced with a link to event.links[0] with the name of event.linksnames[0]. To replace the tags I made the following code (in my controller after the events are loaded).
angular.forEach($scope.events, function (value, key) {
var arrayLength = value.links.length;
for (var i = 0; i < arrayLength; i++) {
value.Text = value.Text.replace("[" + i + "]", '<a ng-href"' + value.links[i] + '">' + value.linkNames[i]+'</a>');
}
});
in my view :
<div ng-repeat="e in events">
<span >{{e.Text}} </span><br />
</div
The tag [0],[1] are replaced but the text is still sanitized and in the view the html tags are shown:
<a ng-href"http://example.com/1">Dave</a> added a new company <a ng-href"http://example.com/2">Philips</a>
instead of the desired result:
Dave added a new company Philips
where dave and philips are clickable links.
Notes: I know the user could name his company my[0]company but the link would still be safe. Im using angular 1.6.4
How can I make the replaced parts clickable and keep the non replaced part sanitized?