Reputation: 2387
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>
Upvotes: 2
Views: 9501
Reputation: 635
<textarea [(ngModel)]="sometext"></textarea>
<pre style="font-family: Arial;">{{sometext}}</pre>
This one worked for Angular 8.
Upvotes: 1
Reputation: 1
<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);
}
});
Upvotes: -1
Reputation: 1
.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);
}
});
Upvotes: 0
Reputation: 674
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
Upvotes: 2
Reputation: 30118
There are two problems in your code above:
\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 || '');
};
});
{{}}
, 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.
Upvotes: 3
Reputation: 503
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 />")
});
Upvotes: -1