Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Safe NL2BR filter for Angular, compatible with linky. Transforms newlines ( \n) into HTML <br /> tags.
# nl2br linky-compatible Angular filter
# CoffeeScript Version (available in JS)
#
# Sample usage in html template:
# "xxx | nl2br"
# <div ng-bind-html=" YourString | nl2br "></div>
angular.module('nl2brFilter', []).filter('nl2br', ['$sanitize', ($sanitize) ->
tag = '<br />'
return (msg) ->
# ngSanitize's linky filter changes \r and \n to &#10; and &#13; respectively
msg = (msg + '').replace(/(\r\n|\n\r|\r|\n|&#10;&#13;|&#13;&#10;|&#10;|&#13;)/g, tag + '$1')
return $sanitize(msg)
])
// nl2br linky-compatible Angular filter
// JS Version (available in CoffeeScript)
//
// Sample usage in html template:
// "xxx | nl2br"
// <div ng-bind-html=" YourString | nl2br "></div>
angular.module('myModule')
.filter('nl2br', ['$sanitize', function($sanitize) {
var tag = '<br />';
return function(msg) {
// ngSanitize's linky filter changes \r and \n to &#10; and &#13; respectively
msg = (msg + '').replace(/(\r\n|\n\r|\r|\n|&#10;&#13;|&#13;&#10;|&#10;|&#13;)/g, tag + '$1');
return $sanitize(msg);
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.