I would like to display images in text instead of :) symbols. I have an html + angularjs 14.8. page with code:
<tr ng-repeat="message in messages | filter:searchForMessage" my-directive="message">
<td align="left" ng-class='{sent :message.id_user_sent == hiddenFriendId, recieved: message.id_user_sent != hiddenFriendId}'>
{{message.id_user_sent == hiddenFriendId ? "⇽" : "⇒"}} {{emoji(message.message)}}
</td>
and I got a function emoji to convert :) to image:
$scope.emoji = function(text) {
var getUrlToMessages = "http://"+location.hostname+(location.port ? ':'+location.port: '')+"/";
var emoticons = {
':)' : getUrlToMessages +'img_smile.png',
':(' : getUrlToMessages +'img_sad.png',
':D' : getUrlToMessages +'img_haha.png',
':o' : getUrlToMessages +'img_omg.png'
}, patterns = [], metachars = /[[\]{}()*+?.\\|^$\-,&#\s]/g;
// build a regex pattern for each defined property
for (var i in emoticons) {
if (emoticons.hasOwnProperty(i)){ // escape metacharacters
patterns.push('('+i.replace(metachars, "\\$&")+')');
}
}
// build the regular expression and replace
return text.replace(new RegExp(patterns.join('|'),'g'), function (match) {
return typeof emoticons[match] != 'undefined' ?'<img ng-src="'+emoticons[match]+'"/>' : match;
});
}
the problem is that I have no image as an output in my UI - I got a text only:
<img src="http://192.168.169.1:8182/ChatGUINoSQL/messages/img_smile.png"/>
which looks like a proper tag at UI, but this is a text only (cause it changes < and > symbols to the code).
What should I do to see images instead of this URL text? Thank you!
Fix
Add:
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular-resource.min.js"></script>
<script src="angular-sanitize.min.js"></script>
Update html:
<tr ng-repeat="message in messages | filter:searchForMessage">
<td align="left" ng-class='{sent :message.id_user_sent == hiddenFriendId, recieved: message.id_user_sent != hiddenFriendId}'>
{{message.id_user_sent == hiddenFriendId ? "⇽" : "⇒"}} <div ng-bind-html="emoji(message.message)"></div> <!-- {{ message.message }} -->
</td>
js updated:
// build the regular expression and replace
return text.replace(new RegExp(patterns.join('|'), 'g'), function (match) {
var escape = typeof emoticons[match] != 'undefined' ? '<img src="' + emoticons[match] + '" />' : match;
return $sce.trustAsHtml(escape);
});