David
David

Reputation: 3604

Should angular .value and .constant be used to store initialization values and utility functions?

I have a fairly complex angularJS application built with many modules, factories and controllers.

When this was started I used a parameters.js file to store initialization vars and constants used in the application. Some of these values are constants and others might be updated by the application (language, search date...). This is not an angular element, it's just a js Object with these values inside. My question is, should this be also inside a module? is there any advantage in keeping this inside a module? if so, what`s the best way to do it, using .value and .constant? creating a factory?

Also, similar issue but in this case regarding an utility function js file, where I have some functions to parse dates and some other stuff used in different places in the application. Should this be stored in a module? and again, what would be the way to do it? a factory?

Any tip would be much appreciated. Thanks!

Upvotes: 1

Views: 52

Answers (1)

rrd
rrd

Reputation: 5957

Speaking only from a point of view of doing this, we use something like this, as a constants.js file:

import angular from 'angular';

export default angular
  .module('app.constants', [])
  .constant('someLength', 50)
  .constant('someDateFormat', 'DD MMM YYYY')
  .name

Inject the particular constant wherever you need in your app.

Upvotes: 0

Related Questions