删除 ng-bind-html-unsafe 后,如何注入 HTML? [英] With ng-bind-html-unsafe removed, how do I inject HTML?

查看:15
本文介绍了删除 ng-bind-html-unsafe 后,如何注入 HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 $sanitize 提供程序和 ng-bind-htm-unsafe 指令来允许我的控制器将 HTML 注入 DIV.

I'm trying to use $sanitize provider and the ng-bind-htm-unsafe directive to allow my controller to inject HTML into a DIV.

但是,我无法让它工作.

However, I can't get it to work.

<div ng-bind-html-unsafe="{{preview_data.preview.embed.html}}"></div>

我发现这是因为它已从 AngularJS 中删除(谢谢).

I discovered that it is because it was removed from AngularJS (thanks).

但是如果没有 ng-bind-html-unsafe,我会收到这个错误:

But without ng-bind-html-unsafe, I get this error:

http://errors.angularjs.org/undefined/$sce/unsafe

推荐答案

  1. 您需要确保已加载 sanitize.js.例如,从 https://ajax 加载它.googleapis.com/ajax/libs/angularjs/[LAST_VERSION]/angular-sanitize.min.js
  2. 你需要在你的 app 中包含 ngSanitize 模块例如:var app = angular.module('myApp', ['ngSanitize']);
  3. 你只需要用 ng-bind-html 绑定原始的 html 内容.无需在控制器中执行任何其他操作.解析和转换由 ngBindHtml 指令自动完成.(阅读它是如何工作的部分:$sce).因此,在您的情况下,<div ng-bind-html="preview_data.preview.embed.html"></div> 可以完成这项工作.
  1. You need to make sure that sanitize.js is loaded. For example, load it from https://ajax.googleapis.com/ajax/libs/angularjs/[LAST_VERSION]/angular-sanitize.min.js
  2. you need to include ngSanitize module on your app eg: var app = angular.module('myApp', ['ngSanitize']);
  3. you just need to bind with ng-bind-html the original html content. No need to do anything else in your controller. The parsing and conversion is automatically done by the ngBindHtml directive. (Read the How does it work section on this: $sce). So, in your case <div ng-bind-html="preview_data.preview.embed.html"></div> would do the work.

这篇关于删除 ng-bind-html-unsafe 后,如何注入 HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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