One page is divided to several sections and each section has its own controller. The JSON objects on those sections are related. After some actions performed in one section, the state of a JSON object is changed. The change shall propagate to other sections of the page. One controller can access an object, say $scope.foo, in another controller. I have $scope.foo = ... after an action of the previous controller. The code, however, doesn't sync up the data displayed in another section.
Also, I have another set of JSON objects which is not accessible in the controller. And one object in the controller is one of those objects. How to sync them up? Is the AngularJS observer a good approach for this problem?
Based on Lux's suggestion, I create a service as the following:
angular.module('myApp.services')
.service('FooService', ['NotificationCenter', function (NotificationCenter) {
"use strict";
var foo;
function setFoo(f) {
foo = f;
}
function getFoo() {
return foo;
}
var facade = {
getFoo: getFoo,
setFoo: setFoo
};
return facade;
}]);
And in the controller of displaying the foo data, I have
// A GET web service call
FooService.setFoo(response);
$scope.currentFoo = FooService.getFoo();
And in another controller where the data is altered, I have
// after changing the data and make a PUT web service call which will retrieve the updated data
FooService.setFoo(response);
The data from the first controller isn't updated.