AngularJS – Include Basic Helper Functions

I want to include helper functions in my AngularJS application. I was wondering, How can I do this using a util sort of behavior? Does it get injected into my controllers? Is it going to be global? Declared on the app space or something. The function I wanted to test out was getting in some data and then converting the filename so it was user friendly (spaces removed, hyphens added, lowercase etc) so it could then match an image to be displayed in the ngTemplate. I was wondering what is the best way to achieve this? This is the first way I tried and seems to be a good solution and works well so stuck with it.

Note: I’m thinking a better way could be to create a UTIL service and inject that into each controller. Which is just as easy and better programming.
Note: See A better approach to helper functions using Angularjs custom filters.

“If you need to add some helpers to your application, simply add them inside run().”



Util functions are now available on the $scope (which should be injected into your controller).


And hence they are available to your ngTemplates too like so:

The result

before: United Kingdom
after: /img/country-flags/united-kingdom-smallflag.png


Sam is a passionate web developer from England. In his spare time he enjoys coding, playing chess, reading and investing.

One thought on “AngularJS – Include Basic Helper Functions

Leave a Reply

Your email address will not be published. Required fields are marked *