admin管理员组文章数量:1395023
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=".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>
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./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>
Share
Improve this question
edited May 23, 2017 at 12:24
CommunityBot
11 silver badge
asked Aug 1, 2015 at 8:39
NathanNathan
2,3873 gold badges17 silver badges19 bronze badges
6 Answers
Reset to default 3DEMO
There are two problems in your code above:
- 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 || '');
};
});
- The second problem is that the filter is not enough to render the html. Instead of interpolating the string itself via
{{}}
, use theng-bind-html
as it is intended to render string as an html. Additionally, you have to include thengSanitize
module, forng-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);
}
});
本文标签: javascriptHow do I make a textarea recognize new lines in AngularJSStack Overflow
版权声明:本文标题:javascript - How do I make a textarea recognize new lines in AngularJS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744107379a2591126.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论