angular nl2br filter
/* | |
# 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> | |
</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.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); | |
} | |
}); |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Thanks @kensnyder. Nice catch. This fixes my issue. |
This comment has been minimized.
This comment has been minimized.
Thank you! |
This comment has been minimized.
This comment has been minimized.
Is there a way to use this without allowing all other HTML too?
(this also makes the nonsafe tekst bold.. but what if I don't want that?) |
This comment has been minimized.
This comment has been minimized.
Thanks – this does almost exactly what I need
I suggest |
This comment has been minimized.
This comment has been minimized.
nice one, Thanks |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
How do I use this together with linky filter?
http://stackoverflow.com/questions/28089736/how-to-use-nl2br-and-linky-filter-together-in-angularjs