Angular: Empty Fiddle
http://angularjs.org/
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>
<div ng-controller="MyCtrl">
<button ng-click="setData()">
Set data
</button>
<h1>
Controller1
</h1>
<hr>
<p>
{{data.getContactInfo()}}
</p>
</div>
<div ng-controller="MyOtherCtrl">
<br>
<br>
<h1>
Controller2
</h1>
<hr> {{data.getContactInfo()}}
</div>
JavaScript
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', function($scope, myService) {
$scope.data = myService;
$scope.setData = function() {
myService.setContactInfo('Hello World');
}
});
myApp.controller('MyOtherCtrl', function($scope, myService) {
$scope.data = myService;
});
myApp.service('myService', function() {
this.contactInfo = '';
this.setContactInfo = function (data) {
this.contactInfo = data;
}
this.getContactInfo = function () {
return this.contactInfo;
}
});