I am using Angular JS to get and ng-repeat over Twitter tweets. I needed to highlight parts of the tweet string, like @tag and #hash, so it was suggested I use replace to add DOM wrappers around the things I want to highlight.
The issue is, since I'm not directly outputting to the DOM, and instead ng-repeating over a scope variable, the html tags don't seem to be added to the strings.
Question: How can I append html tags to a JS string?
Directive snippet:
scope.getTweets = function () {
ModulesService.getTweets().success(function (res) {
if (res && Array.isArray(res)) {
scope.tweets = parseTweets(res);
}
});
};
scope.getTweets();
var parseTweets = function (tweets) {
tweets.forEach(function (tweet) {
tweet.text.replace(/(@[^ ]+)/g, '<a class="user">$1</a>').
replace(/(#[^ ]+)/g, '<span class="hash">$1</span>').
replace(/(https?:\/\/[^ ]+)/g, '<a href="$1">$1</a>');
console.log('tweet!', tweet.text); //does not contain altered HTML
});
return tweets;
};
HTML:
<div ng-repeat="tweet in tweets" class="post-body clearfix">
{{tweet.text}}
</div>