Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
90
rated 0 times [  94] [ 4]  / answers: 1 / hits: 19142  / 11 Years ago, thu, may 23, 2013, 12:00:00

I'm building an ecommerce site (based on shopify) and I'm using multiple small angularjs apps to handle things such as a quick shopping cart, wishlists, filtering products and a few other smaller items. I initially used one big application (that had routing and everything), but it was a bit to restrictive when I didn't have a full REST API.



There are a couple of services that I would like to share between the angular apps (the cart service, so I can have a quick add button that will reflect in the mini-cart and such), but I'm not sure of the best way (if there is a way) to go about this. Just sharing a module with the service doesn't keep the same state across the apps.



I tried my hand at it, but I it doesn't seem to update state between both apps. The following is the javascript I tried using. It's also on jsfiddle with accompanying html: http://jsfiddle.net/k9KM7/1/



angular.module('test-service', [])
.service('TestService', function($window){
var text = 'Initial state';

if (!!$window.sharedService){
return $window.sharedService;
}

$window.sharedService = {
change: function(newText){
text = newText;
},
get: function(){
return text;
}
}

return $window.sharedService;
});

angular.module('app1', ['test-service'])
.controller('App1Ctrl', function($scope, TestService){
$scope.text = function(){ return TestService.get() }
$scope.change = function(){ TestService.change('app 1 activated') }
});

angular.module('app2', ['test-service'])
.controller('App2Ctrl', function($scope, TestService){
$scope.text = function(){ return TestService.get() }
$scope.change = function(){ TestService.change('app 2 activated') }
});

var app1El = document.getElementById('app1');
var app2El = document.getElementById('app2');

angular.bootstrap(app1El, ['app1', 'test-service']);
angular.bootstrap(app2El, ['app2', 'test-service']);


Any help would be appreciated


More From » angularjs

 Answers
190

The sharedService is being shared, but one angular app doesn't know that something updated in the other app so it doesn't kick off a $digest. You have to manually tell the $rootScope of each application to start a $digest by calling $rootscope.$apply()



Fiddle: http://jsfiddle.net/pvtpenguin/k9KM7/3/



  angular.module('test-service', [])
.service('TestService', function($rootScope, $window){
var text = 'Initial state';
$window.rootScopes = $window.rootScopes || [];
$window.rootScopes.push($rootScope);

if (!!$window.sharedService){
return $window.sharedService;
}

$window.sharedService = {
change: function(newText){
text = newText;
angular.forEach($window.rootScopes, function(scope) {
if(!scope.$$phase) {
scope.$apply();
}
});
},
get: function(){
return text;
}
}

return $window.sharedService;
});

[#78050] Wednesday, May 22, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckenna

Total Points: 445
Total Questions: 109
Total Answers: 109

Location: Virgin Islands (U.S.)
Member since Sun, May 16, 2021
3 Years ago
;