我有一個指令,並I'm與IMG SRC更換IMG的URL內嵌顯示圖像渲染HTML中angularJS 1.x中
function message($filter) {
var directive = {
restrict: 'EA',
scope: {
data: '='
},
link: function(scope, elem, attrs) {
scope.data.content = scope.data.content.replace(/(https?:\/\/\S+(\.png|\.jpeg|\.jpg|\.gif))/g, "<img src='$1' alt=''>");
},
template: '<span>{{data.nickname}}:</span> {{data.content}}'
};
return directive;
}
而是看到的圖像內嵌I'm看到HTML標籤作爲我正在調查$ sce但我不確定如何在指令中使用它。
正如你所看到的IMG標籤只有當在規定的附加圖片的一端看到一隻網址 – handsome
你說得對,我已經更新了我的答案使用創建NG-如果模板,我添加了「hasImage」屬性。我還添加了一個解釋,爲什麼最好不要在js中做html模板。 – frodo2975