Last active
April 7, 2016 14:11
-
-
Save elgervb/a6a36af0bc24927474baa82539c69c40 to your computer and use it in GitHub Desktop.
TypeScript angular 1.5 filter
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
/** | |
* Wraps a match inside a string between <b>...</b> tags | |
* | |
* Example: | |
* $filter('filtername').('match'); | |
* | |
* Note: make sure you'll render this using ng-bind-html (in a repeater you'll probably must use ng-if as well, or otherwise all nodes will be rendered (empty when no match)) | |
*/ | |
class MarkSelectedFilter { | |
public static factory() { | |
let factory = ($sce: ng.ISCEService) => { | |
return (input: string, match: string) => { | |
let regex = new RegExp(`(${match})`, 'gi'); | |
if (regex.test(input)) { | |
return $sce.trustAsHtml(input.replace(regex, '<b>$1</b>')); | |
} | |
return null; | |
} | |
}; | |
factory.$inject = ['$sce']; | |
return factory; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment