0

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 ? "&#x21FD" : "&#x21D2"}} {{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 ? "&#x21FD" : "&#x21D2"}} <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);
  });
Aleksey Kiselev
  • 331
  • 1
  • 7
  • 21
  • It's probably the `img` your returning. This might help.. http://stackoverflow.com/a/25513186/1339516 – Searching Dec 05 '16 at 00:30
  • @Searching thanks, but it looks like I got a different version or this solution is not compatible to my situation. Anyway, thank you, I made an update – Aleksey Kiselev Dec 05 '16 at 00:47
  • Just in case - this one looks useful, but not working in my solution.. Don't know why: http://stackoverflow.com/questions/19111337/angular-js-ng-repeat-li-items-with-html-content – Aleksey Kiselev Dec 05 '16 at 01:45

1 Answers1

1

Add ngSanitize cdn to your app.

 angular.module('myApp', ['ngSanitize'])// your app

inject in controller

 myApp.controller('MyCtrl', function($scope,$sce) { // your controller

update return statement. Lets mark the html safe to display. $sce

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);
});

update html

<div ng-bind-html="emoji(message.message)"></div>

Let us know if it's still causing issue.

Searching
  • 2,269
  • 1
  • 17
  • 28
  • You have a small typo with extra ) , but the problem is that I got an error: Error: $injector:modulerr Module Error – Aleksey Kiselev Dec 05 '16 at 01:40
  • may be I made something wrong here: `(function () { 'use strict'; angular.module('myAppName', ['ngResource']).controller('ContactController', ['$scope', '$sce', '$http', '$interval', function($scope, $sce, $http, $interval) {` ? – Aleksey Kiselev Dec 05 '16 at 01:41
  • sorry that was a extra ) in the `var escape =` . Did you link to the cdn you will need to load https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.9/angular-resource.min.js script or from you local. You will need ngResource. @AlekseyKiselev Your declaration looks good. – Searching Dec 05 '16 at 01:44
  • thank you, I have no errors any more, but no I have nothing in output. Here is my code: ` {{message.id_user_sent == hiddenFriendId ? "⇽" : "⇒"}}
    `
    – Aleksey Kiselev Dec 05 '16 at 02:16
  • @AlekseyKiselev Can you inspect the element to see if the `img` tag has been added correctly ? Update this code bit in the question. kinda hard to read... – Searching Dec 05 '16 at 02:21
  • @AlekseyKiselev So the `img` tag wasn't on the page ? – Searching Dec 05 '16 at 02:40
  • I put my updated code with EDIT 2 header. Answer on your question - Nope, it throws an error: https://docs.angularjs.org/error/$sce/unsafe – Aleksey Kiselev Dec 05 '16 at 02:45
  • @AlekseyKiselev just saw you are using 1.4.8, so make sure ngResource is also 1.4.8 https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular-resource.min.js instead of 1.5.9. – Searching Dec 05 '16 at 02:57
  • Base on debug - escape = "" when text = :). But I still got an error https://docs.angularjs.org/error/$sce/unsafe – Aleksey Kiselev Dec 05 '16 at 03:55
  • Boom! Fixed! Just added `` and made an updated for module: `angular.module('myAppName', ['ngResource','ngSanitize']).` @Searching, Thank you so much!!! – Aleksey Kiselev Dec 05 '16 at 04:06
  • @AlekseyKiselev Oh I thought it was a part of `ngResource`. Sorry about that man.. – Searching Dec 05 '16 at 05:24
  • oh, not a problem :) You really helped me, issue resolved and that's the main thing – Aleksey Kiselev Dec 05 '16 at 14:36