# Listen on state changes

To listen on state changes, you need to attach a "hook" in the store using the `hook` method. The hook will trigger depending on what action query you passed in it.

To see that in action, here is the simple example of a hook that queries the actions `INCREMENT_COUNT` and `DECREMENT_COUNT`.

{% code title="your-controller.js" %}

```javascript
import { CounterStore } from 'counter-store';

function YourController($scope, counterStore) {
  counterStore.hook('INCREMENT_COUNT', (state) => {
    $scope.count = state.count;
  });

  counterStore.hook('DECREMENT_COUNT', ({ count }) => {
    $scope.count = count;
  });
}

angular
  .module('App', [])
  .controller('YourController', YourController);
```

{% endcode %}

{% hint style="warning" %}
All the attached hook has an initial phase which makes its callback to run automatically after successfully registered.
{% endhint %}

{% hint style="info" %}
To check if the hook callback is currently running on its initial phase, you can use the second argument from the callback function which determines if it is an initial phase.
{% endhint %}

As you can notice in the first example, you update the same scope variable (which is the `count`) using two different hooks that listen on `INCREMENT_COUNT` and `DECREMENT_COUNT`, you can simplify that by using a wild card, array, or regular expression action query.

{% code title="your-controller.js" %}

```javascript
import { CounterStore } from 'counter-store';

function YourController($scope, counterStore) {
  // Using an array of action
  counterStore.hook(['INCREMENT_COUNT', 'DECREMENT_COUNT'], (state) => {
    $scope.count = state.count;
  });
  
  // Using a wild card
  counterStore.hook('*', (state) => {
    $scope.count = state.count;
  });
  
  // Using a regular expression
  counterStore.hook(/^(IN|DE)CREMENT_COUNT$/, (state) => {
    $scope.count = state.count;
  });
}

angular
  .module('App', [])
  .controller('YourController', YourController);
```

{% endcode %}

{% hint style="warning" %}
Be careful of using wild card (\*) action query. It makes the hook callback to always responds to all dispatched actions, even the one you are not expecting.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://angularjs-store.gitbook.io/docs/tutorials-with-javascript/listen-on-state-changes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
