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.