如何绑定在angularjs HTML [英] How to bind html in angularjs

查看:150
本文介绍了如何绑定在angularjs HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有范围variable.like

I have scope variable.like

$scope.demo='<h1>This is test</h1>';

我可以使用绑定HTML NG-绑定-HTML

<p ng-bind-html="demo"></p>

其工作正常,但怎么可以绑定在 {{..}}

推荐答案

您需要使用的 $ SCE 服务,需要 ngSanitize 模块注入在您的应用程序。

You need to sanitize the html using $sce service, which needs ngSanitize module injected in your app.

基本上你需要允许HTML作为 trustAsHtml mehthod $ SCE 服务。

Basically you need to allow html as trustAsHtml mehthod of $sce service.

code

$scope.demo = $sce.trustAsHtml('<h1>This is test</h1>');

更好的办法

有关使它更更好,你可以创建一个自定义过滤器,并重复使用code

For making it more better you could create an custom filter and reuse that code

app.filter('trsustedhtml', function($sce) { 
    return $sce.trustAsHtml; 
});

标记

<p ng-bind-html="demo| trsustedhtml"></p>

这篇关于如何绑定在angularjs HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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