AngularJS textarea的断行/换行转换 [英] AngularJS textarea linebreak/newline conversion

查看:2847
本文介绍了AngularJS textarea的断行/换行转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有过NG-模型填充内容的textarea的:

I have a textarea with content populated through ng-model:

<textarea data-ng-model="messageBody"></textarea>

消息体需要被存储在使用换行字符(\\ n)的,而不是换行符数据库。然而,当我使用正则表达式转换换行符断行,并通过对模型,角脱HTML。有没有办法直接通过HTML模型或textarea的内容而无需转换?或者,有没有更好的方式来textarea的换行符和换行符之间的转换?

messageBody needs to be stored in the database using newline characters (\n) instead of line breaks. However, when I use regex to convert newline to linebreak and pass that to the model, angular escapes the HTML. Is there a way to pass the HTML directly to the model or textarea without it being converted? Or, is there a better way to convert between textarea line breaks and newline characters?

更新

在进一步的检查,这不是一个角度的问题。我们正在使用填充SQL脚本数据库(Postgres的)。原来插入数据库的字符串没有逃脱。由于字符串包含一个换行符,这意味着他们被直接插入文本。然后,换行从前端处理似乎是从生产什么最初填充不一致的结果。长话短说 - 我逃过了最初的数据库内容,以适应换行符,一切工作正常。

On further inspection, this wasn't an angular problem. We are populating the database (Postgres) using an SQL script. Turns out the strings inserted into the database weren't escaped. Since the strings contained a newline character, that means they were being inserted directly as text. Then, line break handling from the front end seemed to be producing inconsistent results from what was initially populated. Long story short - I escaped the initial database content to accommodate for the newline character, and everything works fine.

推荐答案

一个textarea里面的新行得到的字符串中的新行( \\ n )。

New lines inside a textarea are new lines in the resulting string (\n).

小提琴演示这种行为。

如果你想存储 \\数据库ñ,没有什么做的,该字符串是已经是格式。

If you want to store \n in the database, there is nothing to do, the string is already of that format.

如果通过'断行,你的意思是&LT; BR&GT; 标签,你实际上意味着该字符串来自包含这样的标签数据库,那么你应该替换它们事先我想。

If by 'linebreak', you mean the <br> tag and you actually meant that the string comes from the DB containing such tags, then you should replace them beforehand I suppose.

在理想情况下你的数据库应该包含换行符( \\ n ),你应该保存和获取新行从原样,没有任何转换。

Ideally your database should contain newlines (\n), you should save and fetch newlines from it as is, no conversion.

这篇关于AngularJS textarea的断行/换行转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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