如何显示angularjs模板,而不是字符串的html?

编程入门 行业动态 更新时间:2024-10-28 20:17:48
本文介绍了如何显示angularjs模板,而不是字符串的html?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个变量在我的范围:

I have a variable in my scope:

$scope.myvar = "Hello<br><br>World"

在我的模板我使用的:

<div>{{myvar}}</div>

问题是MYVAR显示文字文本,而我想让它显示的换行符。如何做到这一点?请注意,我想让它这样的,如果我在未来,MYVAR得到与其他HTML更新,然后显示在页面上应该是什么编译HTML而不是文字串在它的HTML标记。

The issue is myvar shows the literal text, whereas I want it to show the line breaks. How to do this? Note that I want to make it such that if I in the future, myvar gets updated with other HTML, then what is shown on the page should be the "compiled" html as opposed to the literal string with the html tags in it.

推荐答案

您可以使用的 ngBindHtml 作为这一点。结果请记住,由于角度的 严格的概念逃逸 内容必须要么消毒(使用产生额外的模块 ngSanitize )或明确地trustedAsHtml(使用 $ sce.trustAsHtml())。后者被认为只为你内容要使用知道是安全的(例如没有用户自定义),反正是不推荐的。

You can use ngBindHtml for that. Keep in mind that due to Angular's Strict Conceptual Escaping the content needs to be either sanitized (using the additonal module ngSanitize) or explicitely "trustedAsHtml" (using $sce.trustAsHtml()). The latter is supposed to be used only for content you know is safe (e.g. nothing user defined) and is not recommended anyway.

注意: ngSanitize 是一个非核心模块,并应分别包括:结果    &LT; SCRIPT SRC =... / angular.min.js&GT;&LT; / SCRIPT&GT; 结果    &LT; SCRIPT SRC =... /角sanitize.min.js&GT;&LT; / SCRIPT&GT;

Note: ngSanitize is an non-core module and should be included separately: <script src=".../angular.min.js"></script> <script src=".../angular-sanitize.min.js"></script>

例如:

/* Using ngSanitize */ angular.module('app', ['ngSanitize']) .controller('myCtrl', function ($scope) { $scope.myHtml = 'Hello<br /><br />world !'; }); /* Using $sce.trustAsHtml() */ angular.module('app', []) .controller('myCtrl', function ($sce, $scope) { $scope.myHtml = $sce.trustAsHtml('Hello<br /><br />world !'); });

需要注意的是 ngSanitize 将过滤不恰当的内容,而 $ sce.trustAsHtml 将允许任何东西。

Note that ngSanitize will filter "non-appropriate" content, while $sce.trustAsHtml will allow anything.

又见这个 短演示

See, also, this short demo.

更多推荐

如何显示angularjs模板,而不是字符串的html?

本文发布于:2023-06-13 04:27:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/674109.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   而不是   模板   angularjs   html

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!