2

I need to share data between controllers. My data is actually an array. I am able to share the data successfully but I have one more requirement. I need to clear this array from one of the controller. I have written a function sharedService.clear() in my sharedService. But that doesn't work. What am I doing wrong here?can anyone please help.

services.service('sharedProperties', function () {
   var sharedService =  {};

    sharedService.personArray = [];

    sharedService.setPersonArray = function(newObj) {
       this.personArray.push(newObj);
    };
    sharedService.getPersonArray = function(){
        return this.personArray;
    };

    sharedService.clear = function(){
        this.personArray = [];
    };
    return sharedService;

});
5
  • Is this.personArray1.push(newObj); a typo? Should be this.personArray.push(newObj); Commented Feb 6, 2014 at 23:42
  • yes typo. Sorry I have updated it. Commented Feb 6, 2014 at 23:46
  • service is built around the this object, factory is buod around the return. I believe you are mixing these up. Commented Feb 6, 2014 at 23:52
  • Thanks @Matthew.Lothian. Can you please tell me the right way of doing it. Commented Feb 7, 2014 at 0:51
  • @user911 ok i gave it a bit of an explanation bellow Commented Feb 7, 2014 at 2:01

2 Answers 2

4

It looks like you've confused a factory with a service in the way you have defined it. Try the below code instead:

services.service('shareProperties', function() {
 this.personArray = [];

 this.clear = function() {
  this.personArray = [];
  }
});

Also, see AngularJS: Service vs provider vs factory for more details

Sign up to request clarification or add additional context in comments.

5 Comments

do you mean I don't need the declaration var sharedService = {};
If I remove this and try your code , while accessing sharedService like this sharedProperties.getPersonArray(); I get error 'unresolved method or function getPersonArray()'.
No, and you do not need to return an object either. Read the link that I posted for a deeper explanation about the differences between factory, service and provider
I didn't reimplement all your functions. If you want access to the personArray, just use: shareProperties.personArray
Thanks!! I a new to Angular and messed up with services and factory. Thank you so much for clearing the concept.
3

From what you have explained you want a static service that shares person across controllers

services.service('sharedProperties', [function () {

    this.personArray = [];

    this.setPersonArray = function(newObj) {
       this.personArray.push(newObj);
    };
    this.getPersonArray = function(){
        return this.personArray;
    };

    this.clear = function(){
        this.personArray = [];
    };

}]);

Anything declared on the this object will be available when referencing the sharedProperties service. Declaring something with var will make it private to the sharedProperties scope and only be available from within the service.

in the first example getPersonArray will return a reference to personArray and i could change or edit the value of sharedProperties and by reference personArray anyway i want making the access methods pretty meaningless.

So you might instead do this to protect your personArray

services.service('sharedProperties', [function () {

    // private
    var personArray = [];

    this.setPersonArray = function(newObj) {
       personArray.push(newObj);
       return [].concat(personArray);
    };
    this.getPersonArray = function(){
        // will return a copy of the personArray at the time of request
        return [].concat(personArray);
    };    
    this.clear = function(){
        personArray = [];
        return [].concat(personArray);
    };

}]);

This way you can only edit the private personArray with your methods. But it does require you to call getPersonArray() to sync any changes between controllers.

I tend to use factories for instance objects or constructor function rather than static like objects.

4 Comments

why there is a return statement in setPersonArray and clear? I thought it would be only for getPersonArray.
because we are updating the internal personArray then returning a copy of it. If the value of sharedProperties is changed from the outside it will not affect the private personArray. So when you set or clear sharedProperties we return the updated sharedProperties
Thanks!! your answer is very detailed and have helped me a lot in clearing my concepts. I want to accept it but I still have this last issue. While accessing this shared service in my controller , I get error 'unresolved method or function getPersonArray()'.If I change it back to my old code, I can access getPersonArray(). What's wrong? myApp.controller('SubmitController' , ['$scope' , 'sharedProperties', function($scope , sharedProperties ){ $scope.submitGridData = function( ) { alert(sharedProperties.getPersonArray()); }; }]);
Thanks!! I have accepted your answer because this one gives more information to a novice like me.

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.