<...">

AngularJS textarea linebreak / newline conversion

I have textarea with content populated by ng-model:

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

messageBody must 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 this to the model, angular speeds up the HTML. Is there a way to pass HTML directly to a model or text field without converting it? Or is there a better way to convert between textarea line breaks and newline characters?

UPDATE

Further verification is not an angular problem. We populate the database (Postgres) with an SQL script. It turns out that the rows inserted into the database were not escaped. Since the strings contain a newline, this means that they were inserted directly as text. Then, handling the line breaks from the front seemed to produce inconsistent results from what was originally populated. In short, I escaped the original contents of the database to place a newline, and everything works fine.

+4
source share
2 answers

New lines within the text field are new lines in the resulting line ( \n).

fiddle .

\n , , .

"linebreak", <br>, , , , .

(\n), , , .

+1

, AngularJS "SCE/Strict Contextual Escaping". .

, , $sce.trustAsHtml() HTML, ng-bind-html DOM. , :

myApp.controller( 'contentCtrl', [ '$scope', '$sce', function( $scope, $sce ){
  $scope.messageBody = $sce.trustAsHtml( 'Hello <br> World' );
}]);

:

<div ng-bind-html="messageBody"></div>

$sce , AngularJS.

+1

Source: https://habr.com/ru/post/1546013/


All Articles