简体   繁体   中英

How do I make a textarea recognize new lines in AngularJS?

Here's a simple example of what I'm trying to put into an angular app. I have a textarea whose text will be output to another part of the page, and if newlines are made in the textarea, I'd like those to transfer over as well. I've tried the answer given here , but that case wasn't using angular and it doesn't seem to be working for me (I tried it by using {{sometext.replace(/\\n/g, "<br />")}} but that just breaks the expression and just displays the expression as a string with curly braces and all. How can I fix this?

 var app = angular.module('SomeApp', []); app.controller('SomeController', function($scope){ $scope.sometext = "Some text\\nMore text"; });
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <body ng-app='SomeApp'> <div ng-controller='SomeController'> <textarea ng-model='sometext' rows='8' cols='25'></textarea> <p>{{sometext}}</p> </div> </body>

DEMO

There are two problems in your code above:

  1. You are trying to render a \\n as a new line in an html page, html doesn't recognize it as an html.

To solve this, you must replace all the \\n within the string when rendering by creating a filter. The filter below uses an array of regular expression and value pair that replaces the string that you want to render.

JAVASCRIPT

  .value('HTMLIZE_CONVERSIONS', [
    { expr: /\n+?/g, value: '<br>' }
  ])

  .filter('htmlize', function(HTMLIZE_CONVERSIONS) {
    return function(string) {
      return HTMLIZE_CONVERSIONS.reduce(function(result, conversion) {
        return result.replace(conversion.expr, conversion.value);
      }, string || '');
    };
  });
  1. The second problem is that the filter is not enough to render the html. Instead of interpolating the string itself via {{}} , use the ng-bind-html as it is intended to render string as an html. Additionally, you have to include the ngSanitize module, for ng-bind-html to render html properly without problems.

HTML

<p ng-bind-html="someText | htmlize"></p>

NOTE

You can add more expression and value pairs in the HTMLIZE_CONVERSIONS array.

You can use another textarea instead of p

<textarea ng-model="sometext"> "you can type your input here"</textarea>

<textarea readonly> {{sometext}} </textarea> 

first line gets your input while the second just just shows the input

<textarea [(ngModel)]="sometext"></textarea> <pre style="font-family: Arial;">{{sometext}}</pre>

This one worked for Angular 8.

.filter('nl2br', function ($sce) {
        return function (msg, is_xhtml) {
            var is_xhtml = is_xhtml || true;
            var breakTag = (is_xhtml) ? '<br />' : '<br>';
            var msg = (msg + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
            return $sce.trustAsHtml(msg);
        }
    });

Try this:

var app = angular.module('SomeApp', []);

app.controller('SomeController', function($scope){
   $scope.sometext = "Some text\nMore text";
   $scope.sometext = $scope.sometext.replace(/\n/g, "<br />")
});

<p ng-bind-html="someText | nl2br"></p>

.filter('nl2br', function ($sce) {

        return function (msg, is_xhtml) {

            var is_xhtml = is_xhtml || true;

            var breakTag = (is_xhtml) ? '<br />' : '<br>';

            var msg = (msg + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');

            return $sce.trustAsHtml(msg);

        }

    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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