I will learn how to share data between controllers, but I am facing some problems.
I have this html view:
<div ng-app="MyApp">
<div ng-controller="firstCtrl">
<input type="text" ng-model="data.message"/>
{{data.message}}
</div>
<div ng-controller="secondCtrl">
<input type="text" ng-model="data.message"/>
{{data.message}}
</div>
<div ng-controller="thirdCtrl">
<input type="text" ng-model="data.message"/>
{{dataTwo.messageTwo}}
</div>
</div>
My script looks like this:
var myApp = angular.module("MyApp",[]);
myApp.service("Data", function() {
return {
message : "Hello World",
}
});
function firstCtrl($scope, Data) {
$scope.data = Data;
};
function secondCtrl($scope, Data) {
$scope.data = Data;
};
function thirdCtrl($scope, Data) {
$scope.data = Data;
$scope.dataTwo = {
messageTwo : $scope.data.message
};
};
I connect my controllers using the "Service". Everything works well, but in the third controllers "dataTwo.messageTwo" does not change when I pass the new value to the input field. The value of dataTwo.messageTwoi is all the time ("Hello World").
What am I doing wrong?
source
share