In an AngularJS application (main) I have an iframe
inside which there is another AngularJS application (iframe) also under my control. I would like to share data between two services, one in the main application and one in the iframe application. They both need to read and write to the same object.
// main
// ... routes ...
views: { main: {
controller: function ($scope, serviceA) {
$scope.serviceA = serviceA;
},
templateUrl: 'iframe.html'
}
// ...
function ServiceA () {
this.sharedData; // exposed to controllers in main app
}
// ...
// iframe
// ...
function ServiceB () {
this.sharedData; // exposed to controllers in iframe app
}
// ...
When inside a controller in iframe application I managed to reference serviceA.sharedData
like this:
var self = this;
var parentScope = $window.parent.angular.element($window.frameElement).scope();
parentScope.$watch('serviceA.sharedData', function (newValue, oldValue) {
self.sharedData = newValue;
}
Can this be achieved and how?
I have read the following, but could not turn it into a solution, yet: