简体   繁体   English

Angular:如何在花括号内的字符串中呈现换行符?

[英]Angular: how to render line breaks in a string inside curly braces?

Maybe this is not possible but I wonder if I can embed a line break in a string I pass to MatDialogComponent displayed on a form via {{ }}也许这是不可能的,但我想知道是否可以在通过{{ }}传递给表单上显示的 MatDialogComponent 的字符串中嵌入换行符

I designed a simple WarningDialogComponent that has params for title, message, OK and cancel button text.我设计了一个简单的WarningDialogComponent ,它具有标题、消息、确定和取消按钮文本的参数。

The message text is displayed inside the template消息文本显示在模板内

<mat-dialog-content>
    {{message}}
</mat-dialog-content>

This works fine except if I want the message to be have defined line breaks, something like:这很好用,除非我希望消息具有已定义的换行符,例如:

You are about to delete account 'Electricians'您即将删除帐户“电工”

This cannot be undone!这不能被撤消!

I tried using backtic quotes, or \n\n embedded in message and I know html is normally escaped.我尝试使用反引号,或\n\n嵌入message中,我知道 html 通常会被转义。 Of course I could redesign my form, or instruct it to render embedded html, but was hoping there is a simple way to somehow put a line break in the string contained in the message variable that would work without a having to tinker with the component itself.当然,我可以重新设计我的表单,或者指示它呈现嵌入式 html,但希望有一种简单的方法可以以某种方式在message变量中包含的字符串中放置一个换行符,而无需修改组件本身.

You could do some css stuff.你可以做一些css 的东西。 mat-dialog-content element automatically gets the mat-dialog-content class. mat-dialog-content 元素自动获取mat-dialog-content class。 Simply add this style to this class in your global stylesheet只需将此样式添加到全局样式表中的此 class

.mat-dialog-content{white-space:pre;}

Now your \n shall be considered as new line in your plain text.现在您的\n应被视为纯文本中的新行 Please refer this simple stackblitz .请参考这个简单的stackblitz

Thanks.谢谢。

it sounds like what you are looking to do is embed html in your variable and have angular display that html without sanitizing it.听起来你想要做的是在你的变量中嵌入 html 并让 angular 显示 html 而不对其进行消毒。 so that your your {{message}} could have line breaks <br/> and have them rendered in the message.以便您的{{message}}可以有换行符<br/>并将它们呈现在消息中。

by default Angular sanitizes the contents of variables to prevent cross site scripting.默认情况下 Angular 会清理变量的内容以防止跨站点脚本。

however, if the value you wish to display is 'trusted' (ie it isn't one that a user can enter), angular allows you to use the Dom sanitizer to mark content as safe.但是,如果您希望显示的值是“可信的”(即它不是用户可以输入的值),angular 允许您使用Dom sanitizer将内容标记为安全。

by using this you can have your message contain HTML and have it rendered as you would expect.通过使用它,您可以让您的message包含 HTML 并按照您的预期进行渲染。 however, for anything that you are looking to bypass the Angular sanitizer with, be sure you control where that message is coming from但是,对于您希望绕过 Angular 消毒剂的任何事情,请确保您控制该消息的来源

It seems you could use innerHTML property instead curly braces.看来您可以使用 innerHTML 属性而不是花括号。 Then you can use the tag <br/>然后你可以使用标签<br/>

<mat-dialog-content [innerHTML]="message"></mat-dialog-content>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM