-
-
Save habovh/3dcb42d18975375d5247 to your computer and use it in GitHub Desktop.
Safe NL2BR filter for Angular, compatible with linky. Transforms newlines ( \n) into HTML <br /> tags.
This file contains hidden or 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
# 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 and respectively | |
msg = (msg + '').replace(/(\r\n|\n\r|\r|\n| | | | )/g, tag + '$1') | |
return $sanitize(msg) | |
]) |
This file contains hidden or 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
// 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 and respectively | |
msg = (msg + '').replace(/(\r\n|\n\r|\r|\n| | | | )/g, tag + '$1'); | |
return $sanitize(msg); | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment