Before this is marked as a duplicate, I read quite a lot of similar questions, but all the answers that I found seem to use $ scope, and after reading the documentation I'm not quite sure that I understand $ scope or why I will use it in this situation.
I found this tutorial that describes how to do what I'm trying to do.
However, it uses a data array. I just need one solid variable. Also, I do not know why it declares an additional object for the factory service that it creates; why not just use factory as an object?
I thought I could do something like this, but I'm not sure if this will work or not.
Creating my factory / service:
var demoModule = angular.module("demoModule", []);
demoModule.factory("demoService", function() {
var demoSharedVariable = null;
return demoSharedVariable;
});
Access to a shared variable in each controller:
var demoControllerOne = demoModule.controller("demoContollerOne", function(demoSharedVariable) {
this.oneFunction = function(oneInput){
demoSharedVariable = oneInput;
};
});
var demoControllerTwo = demoModule.controller("demoContollerTwo", function(demoSharedVariable) {
this.twoFunction = function(twoInput){
demoSharedVariable = twoInput;
};
});
Will this method produce a shared variable that I am after?
source
share