简体   繁体   English

结合使用ng-src和ng-bind-html

[英]Using ng-src with ng-bind-html

I am trying to show some image from dynamic ng-src using ng-bind-html . 我正在尝试使用ng-bind-html从动态ng-src显示一些图像。 Here is the plunker: http://plnkr.co/edit/EmHSUEbVTdUT7cqJ0nX6?p=preview 这是the人: http ://plnkr.co/edit/EmHSUEbVTdUT7cqJ0nX6?p=preview

But the image isn't rendered. 但是图像没有被渲染。 How to get it worked? 如何使其工作?

The purpose is, that I want to load some html-elements from DB and show them using ng-bind-html . 目的是,我想从数据库加载一些html元素,并使用ng-bind-html Sometimes those elements are images (contain ng-src attribute) and sometimes they are <i></i> elements (which get rendered correctly). 有时这些元素是图像(包含ng-src属性),有时它们是<i></i>元素(正确呈现)。

Use $interpolate : 使用$interpolate

PLUNKER 朋克

app.controller("myCtrl", function($scope, $sce, $interpolate) {
    $scope.image = "https://upload.wikimedia.org/wikipedia/commons/5/57/Lorem_Ipsum_Helvetica.png";

    $scope.myText = $interpolate("My name is: <h1>John Doe</h1>" + "<img src='{{image | trustUrl}}'/>")($scope);
    $scope.trustedHtml = $sce.trustAsHtml($scope.myText);
});

From documentation : 文档

$interpolate Compiles a string with markup into an interpolation function. $interpolate将带有标记的字符串编译为插值函数。 This service is used by the HTML $compile service for data binding HTML $ compile服务使用此服务进行数据绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM