Created
December 13, 2013 18:01
-
-
Save noeticpenguin/7948426 to your computer and use it in GitHub Desktop.
example filter with trust as html for angular 1.2+
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
app.filter('Autolink', ['$sce', function($sce){ | |
var urlPattern = /(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/gi; | |
return function(text, target, otherProp) { | |
if(text === undefined || | |
text === null ) { return $sce.trustAsHtml(text);} | |
angular.forEach(text.toString().match(urlPattern), function(url) { | |
text = text.replace(url, "<a target=\"" + target + "\" href="+ url + ">" + url.substring(0,30) +"</a>"); | |
}); | |
return $sce.trustAsHtml(text); | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment