Get the current state

After creating a store, let's try to consume it into our app. To get the current state we need to use the copy method of the store.

In this example we inject the store into the app controller to populate the scope variable count.

controller-a.js
angular
  .module('App', [])
  .controller('ControllerA', function ControllerA($scope, CounterStore) {
    const counterState = CounterStore.copy();
    $scope.count = counterState.count;
  });

We can also pass a string to the copy method to directly access the state property. Just like this example.

controller-a.js
angular
  .module('App', [])
  .controller('ControllerA', function ControllerA($scope, CounterStore) {
    $scope.count = CounterStore.copy('count');
  });

All returned data by the copy method are just only a new state copy. Any changes to that copy does not reflect to other copy and most importantly, it does not reflect to the original state in the store.

Last updated