A better approach to helper functions using Angularjs custom filters A better approach which I think is better programming is to use a custom filter like this:
tofilename_filter.js
1 2 3 4 5 6 7 8 9 10 11 |
//converts string to hyphenated lowercase angular.module("app").filter('toFilename', function() { return function(str) { return str .toLowerCase() .replace(/ /g,'-') .replace(/[^w-]+/g,''); }; }); |
Then in your template you would call it.
1 2 3 |
{{ blog.country | toFilename }} |
If you want to inject it into your controller use this.
1 2 3 4 5 6 |
angular.module("app").controller("myController", function ($scope, toFilenameFilter) { ... toFilenameFilter(params); } |
One thought on “Helper functions using Angularjs custom filters”