How to get line break within string interpolation in Angularjs
Here are two demonstrably working versions...
White SpaceSolution One... if you want newlines to be respected in HTML... (works with the back-tick strings, or with 'My \ntitle'
...
document.getElementById('example').innerHTML = `Mytitle`;
h1 { white-space: pre;}
<h1 id="example">
</h1>
Adding line breaks when using interpolation
The solution, for Angular v2.1.1 at least, is to use [innerHTML]="myAlertMessage"
. It isn't necessary to use "bypassSecurityTrustHtml" for line breaks or lists to work.
Keep line breaks in HTML string
Preserve line breaks in angularjs
Based on @pilau s answer - but with an improvement that even the accepted answer does not have.
<div class="angular-with-newlines" ng-repeat="item in items">
{{item.description}}
</div>
/* in the css file or in a style block */
.angular-with-newlines {
white-space: pre-line;
}
This will use newlines and whitespace as given, but also break content at the content boundaries. More information about the white-space property can be found here:
https://developer.mozilla.org/en-US/docs/Web/CSS/white-space
If you want to break on newlines, but also not collapse multiple spaces or white space preceeding the text (to render code or something), you can use:
white-space: pre-wrap;
Nice comparison of the different rendering modes: http://meyerweb.com/eric/css/tests/white-space.html
convert new line /n to a line break in angular
This does not replace it, but you can use the CSS attribute white-space: pre-line;
to render the \n in the browser:
https://developer.mozilla.org/en-US/docs/Web/CSS/white-space
div { white-space: pre-line;}
<div>Foo Bar Baz Foo Bar
Baz</div>
AngularJS String with newlines, shown without breaks
Binding an HTML seems a bit unsafe. The directive suggested by CuriousGuy has a bit of extra engineering to correctly escape the HTML.
I find it easier to use a styling rule white-space: pre-line
.
Example at JSFiddle.
See also:
- Mozilla Developer Network documentation for
white-space
- Internet Explorer documentation for
white-space
- W3 documentation for
white-space
Add new line to string in interpolation binding
use ng-bind-html directive.
<div class="col-md-8 col-sm-8">
<span class="test" ng-bind-html="test"></span>
</div>
JS
$scope.getLastAction= function (activity) {
if (activity) {
let desc= activity.Desc;
let first= activity.First;
let res= desc+ '<br>' + first;
$scope.test = res;
}
}
Angular: how to render line breaks in a string inside curly braces?
You could do some css stuff. mat-dialog-content element automatically gets the mat-dialog-content class. Simply add this style to this class in your global stylesheet
.mat-dialog-content{white-space:pre;}
Now your \n shall be considered as new line in your plain text. Please refer this simple stackblitz.
Thanks.
replacing comma with linebreak in a string in an uib-alert doenst work
Use ng-bind-html instead of interpolation:
<div uib-alert ng-repeat="alert in testAlerts" type="{{alert.type}}" ng-bind-html="alert.msg"></div>
Here's a plunkr for demonstration.
Related Topics
Redirect to Url After Form Submit in React
Mobile Safari Sometimes Does Not Trigger the Click Event
I Want a Code to Fill in the Form Automatically
How to Calculate the Sum of Table Column in Angular 2+
Javascript: How to Redirect a Page After Validation
React-Native React-Navigation Undefined Is Not an Object (Evaluating 'S.Value.Startswith')
Drawing Curved Svg Arrow Lines from Div to Div
How to Pass Data from Flask to JavaScript in a Template
How to Modify Chartjs Tooltip So I Can Add Customized Strings in Tooltips
Cannot Read Property of Undefined @Viewchild Not Working Angular 5
Calculate Total Sum of Dynamically Added Items to a Table
Sort Json Data Based on Date and Time
Javascript: Refresh Same Page With Query String from Selects
For Loop With Two Array in JavaScript
Prevent Form Redirect or Refresh on Submit
Regular Expression to Accept Both Positive and Negative Numbers