I have an Angular service like this:
angular.module('MyModule')
.factory('SharedData', function( ){
return {
session_id: undefined,
getSessionId: function () {
return this.session_id;
},
setSessionId: function (new_id) {
this.session_id = new_id;
},
};
}).
controller( 'MyController', ['SharedData', function( SharedData ){
console.log( "Retrieved session id = " + SharedData.getSessionId());
}]);
I called the service earlier in another module, for example:
angular.module( 'bootstrapper' )
factory("AnotherService", function(){
var injector = angular.injector(['MyModule', 'ng']),
shared_data = injector.get('SharedData');
shared_data.setSessionId( getUrlParameter('SESSIONID'));
...
});
The result of console output is undefined.
I think that SharedDatain 'MyController' and SharedDatain 'AnotherService' it is not the same object. However, people all say that Angular services are solitary. Are they real loners?
More about my modules: I used the bootstrapper module to manually load another "MyModule" module:
angular.module( 'bootstrapper' )
.run(function () {
angular.bootstrap(document, ['MyModule']);
});
source
share