AngularJS Store
v3.0.0
v3.0.0
  • Introduction
  • Installation
  • Tutorials
    • Setup a store
    • Get the current state
    • Update the state
    • Get notified on state changes
    • Stop from receiving notifications
  • API Reference
    • NgStore
    • copy
    • dispatch
    • hook
Powered by GitBook
On this page
  1. Tutorials

Get the current state

PreviousSetup a storeNextUpdate the state

Last updated 6 years ago

Was this helpful?

CtrlK

Was this helpful?

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.