首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图像不会通过ng-重复中的函数显示在我的页面上。

图像不会通过ng-重复中的函数显示在我的页面上。
EN

Stack Overflow用户
提问于 2016-12-05 00:10:18
回答 1查看 88关注 0票数 0

我想用文本而不是:)符号来显示图像。我有一个html + angularjs 14.8。页的代码:

代码语言:javascript
复制
<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>

我有一个函数表情符号要把:)转换成图像:

代码语言:javascript
复制
$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;
  });
}

问题是,我在UI中没有图像作为输出--我只得到了一个文本:

它看起来像UI中的适当标记,但这只是一个文本(因为它将<和>符号更改为代码)。

我该怎么做才能看到图片而不是这个URL文本呢?谢谢!

Fix

添加:

代码语言:javascript
复制
<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>

更新html:

代码语言:javascript
复制
<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>

更新的联合材料:

代码语言:javascript
复制
// 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);
  });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-05 01:27:03

ngSanitize cdn添加到应用程序中。

代码语言:javascript
复制
 angular.module('myApp', ['ngSanitize'])// your app

注入控制器

代码语言:javascript
复制
 myApp.controller('MyCtrl', function($scope,$sce) { // your controller

更新返回语句。让我们标记html保险箱来显示。$sce

代码语言:javascript
复制
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);
});

更新html

代码语言:javascript
复制
<div ng-bind-html="emoji(message.message)"></div>

如果它还引起问题的话请告诉我们。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40965290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档