8

I'm a newbie in AngularJS and have faced the issue.

Can I reinject my factory singleton object across all controllers, where it's been injected?

For example:

.factory('medicalCenterService', function(MedicalCenterResource) {

    var medicalCenterService = {};

    medicalCenterService.currentMedCenter = MedicalCenterResource.get();

    medicalCenterService.reloadMedCenter = function() {
        medicalCenterService.currentMedCenter = MedicalCenterResource.get();

        return medicalCenterService.currentMedCenter;
    };

    medicalCenterService.updateMedicalCenter = function(medicalCenter) {
        MedicalCenterResource.updateMedicalCenter(medicalCenter);
        medicalCenterService.currentMedCenter = medicalCenter;
    };

    return medicalCenterService;
})

In MedicalCenterController I get singleton object with medical center when application starts:

function MedicalCenterController($scope, medicalCenterService) {
    $scope.currentMedCenter = medicalCenterService.currentMedCenter;
}

But later I try to edit medical center fields (name, address, etc..) in AccountProfileController

function AccountProfileController($scope, medicalCenterService) {

    $scope.currentMedCenter = medicalCenterService.currentMedCenter;

    $scope.applyMedCenterChanges = function (currentMedCenter) {
        medicalCenterService.updateMedicalCenter(currentMedCenter);
    };
}

And what I'm expecting to have is the object with updated fields. How to return a new instance of my singleton?

Artyom Pranovich
  • 6,814
  • 8
  • 41
  • 60
  • Show how you're using this factory, and show what you mean by _"But, when I edit fields ..."_. – Stewie Dec 27 '13 at 12:49
  • 1
    Possible duplicate of [Non-Singleton Services in Angular](http://stackoverflow.com/questions/16626075/non-singleton-services-in-angular) – Nick Grealy Feb 02 '16 at 12:26

2 Answers2

11

Do you want something like this?

.factory('MedicalCenter', function(MedicalCenterResource) {
    var MedicalCenter = function () {
        var center = MedicalCenterResource.get(),
            update = function() {
                MedicalCenterResource.updateMedicalCenter(center)
            };
        return {
            center: center,
            update: update
        }    
    };
    return MedicalCenter;

})

function MedicalCenterController($scope, MedicalCenter) {
    center = new MedicalCenter();
    $scope.currentMedCenter = center.center;
}
function AccountProfileController($scope, MedicalCenter) {
    center = new MedicalCenter();
    $scope.currentMedCenter = center.center;
    $scope.applyMedCenterChanges = function () {
        center.update();
    };
}
kfis
  • 4,739
  • 22
  • 19
3

Like you wrote in post services are Singletons and its good way to share data over services. However if you want to create new instance of factory/service, you can't do that but we can create list of objects in one service/factory where each list item represents different instance. Something like:

.factory('medicalCenterService', function(MedicalCenterResource) {

    var medicalCenterServices = [
        {ctrlName: 'MedicalCenterController',medicalCenterService: {/*....*/}},
        {ctrlName: 'AccountProfileController',medicalCenterService: {/*....*/}},
    ];

        //......
})
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
  • Is it really, that I don't have possibility to repeatedly get new instance of factory and inject it in all controllers? – Artyom Pranovich Dec 27 '13 at 13:02
  • i fixed a bit the answer, factory is like static utility but if you want to use it as storage, just create list. Add some id per instance – Maxim Shoustin Dec 27 '13 at 13:03