Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何通过AngularJS模板输出html? {{revision.title}_Javascript_Html_Templates_Angularjs_View - Fatal编程技术网

Javascript 如何通过AngularJS模板输出html? {{revision.title}

Javascript 如何通过AngularJS模板输出html? {{revision.title},javascript,html,templates,angularjs,view,Javascript,Html,Templates,Angularjs,View,标题输出很好,但内容却不好。它包含一些html,我得到了以下错误:试图在安全上下文中使用不安全的值。这被描述为:$sce:unsafe,这很好,但是我如何输出内容,因为其中将包含一些html,因此我必须将其设置为{revision.content | safe}或smthn。正确的方法是什么 编辑: AngularJS版本:1.2您使用的是什么版本? 如果您使用的小于1.2,您可以尝试ngBindHtmlUnsafe我创建了一个nghtml指令,它执行与ngbindhtml不安全以前相同的基本操

标题输出很好,但内容却不好。它包含一些html,我得到了以下错误:
试图在安全上下文中使用不安全的值。
这被描述为:$sce:unsafe,这很好,但是我如何输出内容,因为其中将包含一些html,因此我必须将其设置为
{revision.content | safe}
或smthn。正确的方法是什么

编辑:


AngularJS版本:1.2

您使用的是什么版本?
如果您使用的小于1.2,您可以尝试
ngBindHtmlUnsafe

我创建了一个
nghtml
指令,它执行与
ngbindhtml不安全
以前相同的基本操作。但是,我强烈建议您谨慎使用。它很容易使您的站点受到恶意攻击。因此,在实施之前,请先了解您在做什么:

<h1>{{ revision.title }}</h1>

<div ng-bind-html="revision.content"></div>
然后你会把它用作:

.directive('ngHtml', ['$compile', function($compile) {
    return function(scope, elem, attrs) {
        if(attrs.ngHtml){
            elem.html(scope.$eval(attrs.ngHtml));
            $compile(elem.contents())(scope);
        }
        scope.$watch(attrs.ngHtml, function(newValue, oldValue) {
            if (newValue && newValue !== oldValue) {
                elem.html(newValue);
                $compile(elem.contents())(scope);
            }
        });
    };
}]);

希望这能有所帮助。

因此解决方法如下:

从中包含
angular sanitize.min.js
并将其包含在您的模块中:

<div ng-html="revision.content"></div>

然后您可以自由使用
ng bind html

我知道这是一个老问题,但您也可以在控制器中使用
$sce
信任该值:

var app = angular.module('app', ['ngSanitize']);
在此之后,
ngbindhtml
将起作用。

根据您的需要,您必须将ngSanitize注入到您的应用程序依赖项中

计算表达式并将结果HTML插入到 元素以一种安全的方式。默认情况下,生成的HTML内容将 使用$sanitize服务进行消毒。利用这个 功能,确保$sanitize可用,例如 将ngSanitize包含在模块的依赖项中(而不是核心中 角度)。为了在模块的依赖项中使用ngSanitize, 您需要在应用程序中包含“angular sanitize.js”

然后您可以通过以下方式安装
ngSanitize
模块:

1-使用bower

$scope.revision.content = $sce.trustAsHtml($scope.revision.content);
2-使用npm

bower install --save angular-sanitize
3-通过从like下载
angular sanitize.js
文件手动执行


有关安装
ngSanitize
模块的更多信息,请参见

感谢您的指导,非常好!只有最后一行缺少结束符]-应该是
}]2年后,在1.4.x中,这个看似简单的修复仍然有效!当我使用$compile时,它解决了我的问题
npm install --save angular-sanitize