Javascript Angularjs:img src从json加载html时消失

Javascript Angularjs:img src从json加载html时消失,javascript,angularjs,Javascript,Angularjs,在json元素中,假设datahtmlI有 <p><img src=\"images/wwgd.png\" alt=\"wwgd\" /></p> 我认为您需要使用严格的上下文转义$sce。医生 您的示例如下所示: 控制器: .controller('ExampleController', ['$scope', '$sce', function($scope, $sce) { $scope.myHTML = '<p><img s

在json元素中,假设
datahtml
I有

 <p><img src=\"images/wwgd.png\" alt=\"wwgd\" /></p>

我认为您需要使用严格的上下文转义
$sce
。医生

您的示例如下所示:

控制器:

.controller('ExampleController', ['$scope', '$sce', function($scope, $sce) {

    $scope.myHTML = '<p><img src=\"images/wwgd.png\" alt=\"wwgd\" /></p>';  

    $scope.trustDodgyHTML = function(html) {
      return $sce.trustAsHtml(html);
    };  
.controller('ExampleController',['$scope','$sce',函数($scope,$sce){
$scope.myHTML='

'; $scope.trustDodgyHTML=函数(html){ 返回$sce.trustAsHtml(html); };
第页:



这将明确允许您在此处包含此html。

我认为这相当于我在问题中使用的
data ng bind html
?它不起作用。修复-我认为使用$sce会对您有所帮助。@CapsNZ我刚刚注意到,在html中引入此函数,虽然使其起作用,但会引入许多隐藏的错误控制台中显示的URL。您可以发布其中的一些吗?尽管存在错误,但这肯定是您添加动态htmlI的方式。我在$sce页面上发现了这样一条评论:“[href]和img[src]会自动清理它们的URL,并且不会将它们传递给$sce.getTrusted.sce在此处不起作用。”。
  <p><img alt="wwgd"></img></p>
.controller('ExampleController', ['$scope', '$sce', function($scope, $sce) {

    $scope.myHTML = '<p><img src=\"images/wwgd.png\" alt=\"wwgd\" /></p>';  

    $scope.trustDodgyHTML = function(html) {
      return $sce.trustAsHtml(html);
    };  
<div ng-bind-html="trustDodgyHTML(myHTML)"></div>