-
-
Save lemonlatte/3271836 to your computer and use it in GitHub Desktop.
Truncate Filter for AngularJS v1.0
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
// add the filter to your application module | |
angular.module('yourAppName', ['filters']); | |
/** | |
* Truncate Filter | |
* @Param string | |
* @Param int, default = 10 | |
* @Param string, default = "..." | |
* @return string | |
*/ | |
angular.module('filters', []). | |
filter('truncate', function () { | |
return function (text, length, end) { | |
if (isNaN(length)) | |
length = 10; | |
if (end === undefined) | |
end = "..."; | |
if (text.length <= length || text.length - end.length <= length) { | |
return text; | |
} | |
else { | |
return String(text).substring(0, length-end.length) + end; | |
} | |
}; | |
}); | |
/** | |
* Example - see the jsfiddle: http://jsfiddle.net/tUyyx/ | |
* | |
* var myText = "This is an example."; | |
* | |
* {{myText|truncate}} | |
* {{myText|truncate:5}} | |
* {{myText|truncate:25:" ->"}} | |
* | |
* Output | |
* "This is..." | |
* "Th..." | |
* "This is an e ->" | |
* | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment