index.html
<body >
<p ng-controller="MainCtrl as mv">Hello {{mv.name}}!</p>
<hr>
<div ng-controller="MainCtrl2 as mv">
<input type="text" ng-model="name">
<button ng-click="mv.setN(name)">submit</button><br>
Hello {{name}}<br>
Hello {{mv.name}}!
</div>
</body>
app.js :
var app = angular.module('plunker', []);
app.service('myService', function() {
var my = this;
my.name = "original";
});
app.controller('MainCtrl', function(myService) {
var mv = this;
mv.name = myService.name;
});
app.controller('MainCtrl2', function(myService) {
var mv = this;
mv.name = myService.name;
mv.setN = function(a) {
myService.name = a;
};
});
why isn't the service able to establish communication between controllers ?I have seen a similar example of factory which is working for communication. my plunk: http://plnkr.co/edit/L5uRHPQiXqQV6K7twHul?p=preview