Created
July 28, 2014 08:39
-
-
Save theeluwin/e69545a3badd011f5179 to your computer and use it in GitHub Desktop.
useful angular filters
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
| // converts link in text into anchor | |
| app.filter("link_finder", function(){ | |
| return function(input){ | |
| return input? input.replace(/(\bhttps?:\/\/[^\s<>"`{}|\^\[\]\\]+)/g, "<a href='$1' target='new'>$1</a>") : ""; | |
| }; | |
| }); | |
| // escape '<' and '>' | |
| app.filter("escape_html", function(){ | |
| return function(input){ | |
| return input? input.replace(/</g, "<").replace(/>/g, ">") : ""; | |
| }; | |
| }); | |
| // allow <br> only | |
| app.filter("enable_br", function(){ | |
| return function(input){ | |
| return input? input.replace(/<br\/?\ ?>/g, "<br>").replace(/\n/g, "<br>") : ""; | |
| }; | |
| }); | |
| // forcely add 'http(s)?://' infront of link, if not | |
| app.filter("external_link", function(){ | |
| return function(input){ | |
| if(typeof(input) == "string") { | |
| return input.substr(0, 4) == "http"? input : "http://" + input; | |
| } else { | |
| return input; | |
| } | |
| }; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment