Ajax Loader
×
HTML
<body ng-app="myApp">
1
<body ng-app="myApp">
2
<script src="https://rawgit.com/angular/bower-angular/master/angular.min.js"></script>
3
  
4
  <h2>Shared State with Service</h2>
5
  <c1></c1>
6
  <br>
7
  <c2></c2>
8
  <br><br>
9
  <a href="http://sterl.org/2017/06/angularjs-cheat-sheet/">< Back</a>
10
</body>
 
JavaScript
var myApp = angular.module('myApp', []);
1
var myApp = angular.module('myApp', []);
2
//-- service
3
myApp.service('myServiceName', function() {
4
    var privateState = {foo: 'bar'};
5
    this.getState = function () {return privateState;};
6
});
7
 
8
myApp.component('c1', {
9
    controller: ['myServiceName', function (myServiceName) {
10
        this.state = myServiceName.getState();
11
    }],
12
    template: 'C1: <input ng-model="$ctrl.state.foo">'
13
});
14
 
15
myApp.component('c2', {
16
    controller: ['myServiceName', function (myServiceName) {
17
        this.state = myServiceName.getState();
18
    }],
19
    template: 'C2: <input ng-model="$ctrl.state.foo">'
20
});
 

AngularJS Service Shared State

0 Collaborators

Chat

Sign in with github/twitter to Chat
CSSDeck G+