3

I am working on an angularJS app and I am trying to stick with the most efficient and widely accepted styles of development in AngularJs. Currently, I am using this way of declaring my services like so:

app.factory('MyService', function() {
  /* ... */
  function doSomething(){
     console.log('I just did something');

  }

  function iAmNotVisible(){
      console.log('I am not accessible from the outside');
  }
  /* ... */

  return{
     doSomething: doSomething
  };
});

However, there are numerous examples out there and I am not quite sure which design style to follow. Can someone with extensive knowledge about services explain the reason why a certain style is more relevant than another?

Is what I am doing useful in any way other than restricting the access to certain functions in my service?

T J
  • 42,762
  • 13
  • 83
  • 138
Georgi Angelov
  • 4,338
  • 12
  • 67
  • 96

3 Answers3

6

I would suggest you layout your factory or service as they do in the angular-seed app, except that app annoyingly only uses value in the services.js boilerplate. However you can adapt the layout they use for controllers, directives and filters.

'use strict';

/* Filters */

angular.module('myApp.filters', []).
  filter('interpolate', ['version', function(version) {
    return function(text) {
      return String(text).replace(/\%VERSION\%/mg, version);
    }
  }]);

Which means for a service you would do:

'use strict';

/* Services */

angular.module('myApp.filters', []).
  service('myservice', ['provider1', 'provider2', function(provider1, provider2) {
      this.foo = function() { 
          return 'foo'; 
      };
  }]).
  factory('myfactoryprovider', ['myservice', function(myservice) {
       return "whatever";
  }]);

This has more boilerplate than you absolutely need, but it is minification safe and keeps your namespaces clean.

Than all you have to do is decide which of const, value, factory or service is most appropriate. Use service if you want to create a single object: it gets called as a constructor so you just assign any methods to this and everything will share the same object. Use factory if you want full control over whether or not an object is created though it is still only called once. Use value to return a simple value, use const for values you can use within config.

distante
  • 6,438
  • 6
  • 48
  • 90
Duncan
  • 92,073
  • 11
  • 122
  • 156
0

I don't believe there's an accepted standard but I myself follow this convention:

var myServiceFn = function (rootScope, http) { ... };
...
app.factory('MyService', ['$rootScope', '$http', myServiceFn]);

I feel like this is cleaner than defining the function inline and also allows for proper injection if I ever decide to minify my files. (see http://docs.angularjs.org/tutorial/step_05).

Manny D
  • 20,310
  • 2
  • 29
  • 31
  • 1
    where are you defining myServiceFn? if you define it outside, wouldn't that make it global? – Georgi Angelov Jul 29 '13 at 20:42
  • After injecting it into the app, you could follow with a `var myServiceFn = null;` But yeah, why define it outside to begin with if the goal is to keep it private. – williambq Jul 29 '13 at 21:05
0

As an example, I've been defining services within modules like so:

angular.module('userModule', [])
  .service('userService', function() {
    this.user = null;

    this.getUser = function() {
      return this.user;
    };

    this.userIsNull = function() {
      return (this.user === null);
    };

    this.signout = function() {
      this.user = null;
    };
  })

I think it is more clear to use the .service rather than the .factory?

Josh Petitt
  • 9,371
  • 12
  • 56
  • 104
  • Remeber that `service` and `factory` do different things, so it isn't just down to which is clearer. `factory` is called like a normal function, `service` is a constructor called with the `new` operator. – Duncan Jul 30 '13 at 09:41
  • @Duncan, yes agreed. I find that the service is more "service" like? For instance, when you inject to the module into the app, you only get one service, like a singleton object? The factory I use when I want a "factory", i.e. most of my factories only have a single create() function that returns a new object. Often the service call a factory to create new objects for it. I'm still somewhat new to AngularJS, so I may be getting caught up in the design pattern terms and not the AngularJS terms. – Josh Petitt Jul 30 '13 at 17:01