-
-
Save kensnyder/49136af39457445e5982 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
# Usage in html template: | |
"xxx | nl2br" | |
<div ng-bind-html=" YourString | nl2br "></div> | |
or: | |
"xxx | nl2br:Boolean" | |
Boolean( true or flase or just keep null) means is xhtml or not | |
if is xhtml, replace with <br/> ; if not , replace with <br> | |
<div ng-bind-html=" YourString | nl2br:true "></div> | |
------------------------- | |
# Example: | |
//==Analog data=== | |
$scope.items = [ | |
{"message": "test"}, | |
{"message": "New\nLine"}, | |
] | |
//===== | |
<div class="comment" ng-repeat="item in items"> | |
<p ng-bind-html=" item.message | nl2br "></p> | |
<!-- or with linky --> | |
<p ng-bind-html=" item.message | linky | nl2br "></p> | |
</div> | |
------------------------- | |
# Output result: | |
<div class="comment ng-scope" ng-repeat="item in items"> | |
<p class="ng-binding" ng-bind-html=" item.message | nl2br "> | |
test | |
</p> | |
</div> | |
<div class="comment ng-scope" ng-repeat="item in items"> | |
<p class="ng-binding" ng-bind-html=" item.message | nl2br "> | |
New<br>Line | |
</p> | |
</div> | |
*/ | |
angular.module('myModule') | |
.filter('nl2br', ['$sanitize', function($sanitize) { | |
var tag = (/xhtml/i).test(document.doctype) ? '<br />' : '<br>'; | |
return function(msg) { | |
// ngSanitize's linky filter changes \r and \n to and respectively | |
msg = (msg + '').replace(/(\r\n|\n\r|\r|\n| | | | )/g, tag + '$1'); | |
return $sanitize(msg); | |
}; | |
}]); |
I was wondering the same thing as @vpArth...
So I looked up on W3schools:
Differences Between HTML and XHTML
In HTML, the
<br>
tag has no end tag.
In XHTML, the<br>
tag must be properly closed, like this:<br />
.
But HTML will handle the self-closing tag <br />
as well, so for compatibility issues, just don't bother trying to find out in which doctype we are, just use <br />
;-)
Source (Stack Overflow): HTML 5: Is it <br>, <br/>, or <br />?
TL;DR
Always use <br />
.
See my fork here (Bonus: CoffeeScript version)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You forget about boolean is_xhtml variable in your comments ;)
What wrong with using
<br />
everywhere?