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?

2
  • Show how you're using this factory, and show what you mean by "But, when I edit fields ...". Commented Dec 27, 2013 at 12:49
  • 1
    Possible duplicate of Non-Singleton Services in Angular Commented Feb 2, 2016 at 12:26

2 Answers 2

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();
    };
}
Sign up to request clarification or add additional context in comments.

Comments

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: {/*....*/}},
    ];

        //......
})

2 Comments

Is it really, that I don't have possibility to repeatedly get new instance of factory and inject it in all controllers?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.