I'm currently working on establishing a two-way data binding connection between two separate controllers and a shared service in AngularJS:
app.factory("sharedScope", function($rootScope) {
var scope = $rootScope.$new(true);
scope.data = "initial text from factory";
return scope;
});
app.controller("first", function($scope, sharedScope) {
$scope.data1 = sharedScope.data;
});
app.controller("second", function($scope, sharedScope) {
$scope.data2 = sharedScope.data;
});
Fiddle: http://jsfiddle.net/akashivskyy/MLuJA/
Upon launching the application, data1
and data2
are successfully set to the initial text from factory
, but any subsequent changes made to either of them do not reflect across all three scopes.
How can I establish this binding?
P.S. If there's a more efficient approach than returning a scope while still retaining access to event and observation functionalities (without essentially redefining them), please advise. :)