采用了棱角分明的字符串中的HTML实体 [英] Using HTML Entities within Angular strings

查看:134
本文介绍了采用了棱角分明的字符串中的HTML实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于我的$范围模型一个字符串,它包含HTML实体,如何确保实体正确显示为一个HTML字符,而不是一个字符串?

<一个href=\"http://plnkr.co/edit/0BliljcDkj0vvj3jdEqz?p=$p$pview\">http://plnkr.co/edit/0BliljcDkj0vvj3jdEqz?p=$p$pview

 &LT;!DOCTYPE HTML&GT;
&LT; HTML和GT;  &LT; HEAD&GT;
    &LT;脚本数据需要=angular.js@*
            数据semver =1.2.13
            SRC =HTTP://$c$c.angularjs.org/1.2.13/angular.js&GT;&下; /脚本&GT;  &LT; /头&GT;  &LT;身体GT;
    &LT; D​​IV NG控制器=htmlChar&GT;的{{title}}&LT; / DIV&GT;    &LT;脚本&GT;      angular.element(文件)。就绪(函数(){        VAR应用= angular.module(应用程序,[]);
        app.controller(htmlChar功能($范围){
          $ scope.title =&放大器;复制;潇洒;
        });
        angular.bootstrap(文件,['应用']);      });    &LT; / SCRIPT&GT;  &LT; /身体GT;
&LT; / HTML&GT;


解决方案

通过 $ SCE 。你需要明确地告诉角度内容的HTML。

 &LT; D​​IV NG控制器=htmlCharNG绑定-HTML =标题&GT;&LT; / DIV&GT;&LT;脚本&GT;  angular.element(文件)。就绪(函数(){    VAR应用= angular.module(应用程序,[]);
    app.controller(htmlChar功能($范围,$ SCE){
      $ scope.title = $ sce.trustAsHtml(与&amp;复制;潇洒);
    });
    angular.bootstrap(文件,['应用']);  });&LT; / SCRIPT&GT;

<一个href=\"http://plnkr.co/edit/9iNnRC7AxFptnQZLPtYR?p=$p$pview\">http://plnkr.co/edit/9iNnRC7AxFptnQZLPtYR?p=$p$pview

Given a string in my $scope model which contains an HTML entity, how do I ensure that the entity is properly displayed as an HTML character rather than a literal string?

http://plnkr.co/edit/0BliljcDkj0vvj3jdEqz?p=preview

<!DOCTYPE html>
<html>

  <head>
    <script data-require="angular.js@*" 
            data-semver="1.2.13" 
            src="http://code.angularjs.org/1.2.13/angular.js"></script>

  </head>

  <body>
    <div ng-controller="htmlChar">{{title}}</div>

    <script>

      angular.element(document).ready(function(){

        var app=angular.module("app",[]);
        app.controller("htmlChar",function($scope){
          $scope.title = "&copy; Acme";
        });
        angular.bootstrap(document, ['app']);

      });

    </script>

  </body>
</html>

解决方案

With $sce. You need to explicitely tell angular the content is html.

<div ng-controller="htmlChar" ng-bind-html="title"></div>

<script>

  angular.element(document).ready(function(){

    var app=angular.module("app",[]);
    app.controller("htmlChar",function($scope, $sce){
      $scope.title = $sce.trustAsHtml("&copy; Acme");
    });
    angular.bootstrap(document, ['app']);

  });

</script>

http://plnkr.co/edit/9iNnRC7AxFptnQZLPtYR?p=preview

这篇关于采用了棱角分明的字符串中的HTML实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆