-1

I have this service:

.factory('LocalizationService', ['$resource', function ($resource) {

    // Create our service
    var LocalizationService = function (language) {

        // Get a reference to this service
        var self = this;

        // Use default if no language is supplied
        language = language || 'en_EU';

        // Get the path of the json
        var path = '/assets/resources/' + language + '/resource.json';

        // Return our promise
        $resource(path).get(function (response) {

            // Assign our response to our service
            self.resource = response;
        });
    };

    // Return our service
    return LocalizationService;
}])

and to instantiate it I would have to call

var localazation = new LocalizationService();

but I would like to use it as normal, so that it behaves like all other services. I have a validation function that has something like this in it:

// If we have no sport
if (!team.sport) {

    // Display a warning
    errorMessage = errorMessage ? errorMessage + '<br />' + service.localization.sportInvalid.message : service.localization.sportInvalid.message;
}

// If no team name has been set
if (!team.name) {

    // Display a warning
    errorMessage = errorMessage ? errorMessage + '<br />' + service.localization.teamInvalid.message : service.localization.teamInvalid.message;
}

// If no colours have been chosen
if (!team.colour1 || !team.colour2) {

    // Display a warning
    errorMessage = errorMessage ? errorMessage + '<br />' + service.localization.colourInvalid.message : service.localization.colourInvalid.message;
}

now, using the instantiation above would be fine. But if I have my localization service used elsewhere (as I do) I need to make sure that both are using the same language and that someone doesn't accidentally specify something else. If it was a singleton and worked like other services I would be fine. Does anyone know how I can achieve what I am after?

r3plica
  • 13,017
  • 23
  • 128
  • 290

3 Answers3

0

I believe you just need to use .service instead of .factory. See the answer here: angular.service vs angular.factory

Community
  • 1
  • 1
Fresheyeball
  • 29,567
  • 20
  • 102
  • 164
0

As per angular documentation, you can do the instantiation inside the service definition, that way you don't need to invoke new separately -

var myModule = angular.module('myModule', []);
myModule.factory('serviceId', function() {
var shinyNewServiceInstance;
// factory function body that constructs shinyNewServiceInstance
 return shinyNewServiceInstance; /// <<<< create a new instance here
});
Tushar
  • 3,022
  • 2
  • 26
  • 26
0

One more thing, they are all Singletons...

Also keep in mind that in both cases, angular is helping you manage a singleton. Regardless of where or how many times you inject your service or function, you will get the same reference to the same object or function. (With the exception of when a factory simply returns a value like a number or string. In that case, you will always get the same value, but not a reference.)

-- StackOverflow: "angular.service vs angular.factory"

Community
  • 1
  • 1
georgeawg
  • 48,608
  • 13
  • 72
  • 95