随着NG绑定-HTML不安全的删除,我怎么注入HTML? [英] With ng-bind-html-unsafe removed, how do I inject HTML?

查看:98
本文介绍了随着NG绑定-HTML不安全的删除,我怎么注入HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用 $消毒提供商和 NG-绑定-HTM-不安全指令,允许我的控制器注入HTML成一个DIV。

不过,我无法得到它的工作。

 < D​​IV NG绑定HTML的不安全={{preview_data preview.embed.html}}>< / DIV>

我发现,这是因为它是从AngularJS删除(感谢)。

但是,如果没有 NG-结合HTML的不安全,我得到这个错误:结果

<一个href=\"http://errors.angularjs.org/undefined/$sce/unsafe\">http://errors.angularjs.org/undefined/$sce/unsafe


解决方案

  1. 您需要确保sanitize.js被加载。例如,从<一个加载href=\"https://ajax.googleapis.com/ajax/libs/angularjs/[LAST_VERSION]/angular-sanitize.min.js\">https://ajax.googleapis.com/ajax/libs/angularjs/[LAST_VERSION]/angular-sanitize.min.js

  2. 您需要包括 ngSanitize 模块的应用
    例如: VAR应用= angular.module('对myApp',['ngSanitize']);

  3. 您只需要使用 NG-绑定-HTML 原始 HTML 内容绑定。不需要做任何事情在你的控制器。解析和转换是由 ngBindHtml 指令自动完成。 (请仔细阅读本的它是如何工作部分:的 $ SCE )。所以,你的情况&LT; D​​IV NG绑定-HTML =preview_data preview.embed.html。&GT;&LT; / DIV&GT; 会做的工作。

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>

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

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

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

解决方案

  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绑定-HTML不安全的删除,我怎么注入HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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