我想用文本而不是:)符号来显示图像。我有一个html + angularjs 14.8。页的代码:
<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>
我有一个函数表情符号要把:)转换成图像:
$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
添加:
<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:
<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>更新的联合材料:
// 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);
});发布于 2016-12-05 01:27:03
将ngSanitize cdn添加到应用程序中。
angular.module('myApp', ['ngSanitize'])// your app注入控制器
myApp.controller('MyCtrl', function($scope,$sce) { // your controller更新返回语句。让我们标记html保险箱来显示。$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);
});更新html
<div ng-bind-html="emoji(message.message)"></div>如果它还引起问题的话请告诉我们。
https://stackoverflow.com/questions/40965290
复制相似问题