<body ng-app="myApp">
<body ng-app="myApp">
<script src="https://rawgit.com/angular/bower-angular/master/angular.min.js"></script>
<h2>Shared State with Service</h2>
<c1></c1>
<br>
<c2></c2>
<br><br>
<a href="http://sterl.org/2017/06/angularjs-cheat-sheet/">< Back</a>
</body>
var myApp = angular.module('myApp', []);
var myApp = angular.module('myApp', []);
//-- service
myApp.service('myServiceName', function() {
var privateState = {foo: 'bar'};
this.getState = function () {return privateState;};
});
myApp.component('c1', {
controller: ['myServiceName', function (myServiceName) {
this.state = myServiceName.getState();
}],
template: 'C1: <input ng-model="$ctrl.state.foo">'
});
myApp.component('c2', {
controller: ['myServiceName', function (myServiceName) {
this.state = myServiceName.getState();
}],
template: 'C2: <input ng-model="$ctrl.state.foo">'
});