AngularJS Store
v4.0.5
v4.0.5
  • Introduction
  • What's New
  • Installation
  • Tutorials with Javascript
    • Setup a store
    • Get the current state
    • Update the state
    • Listen on state changes
    • Destroy a hook
  • Tutorials with Typescript
    • Setup a store
    • Get the current state
    • Update the state
    • Listen on state changes
    • Destroy a hook
Powered by GitBook
On this page

Was this helpful?

  1. Tutorials with Typescript

Destroy a hook

Whenever you add a hook in the store, it always returns a HookLink instance that represents the link between the hook and store. You can destroy this link anytime you want to stop the hook from listening on state changes.

The returned HookLink instance is only available inside the hook callback after the initial phase.

your-controller.ts
import { CounterState, CounterStore } from 'counter-store';

function YourController($scope: ng.IScope, counterStore: CounterStore) {
  const hookLink = counterStore.hook('INCREMENT_COUNT', (state) => {
    $scope.count = state.count;
    
    if ($scope.count === 10) {
      hookLink.destroy();
    }
  });
}

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

Another method of HookLink is destroyOn. You can use this method if you want to destroy a hook automatically. The only requirement is a scope where hook should be bound. When a scope triggers a $destroy event, the hook also gets destroyed.

your-controller.ts
import { CounterState, CounterStore } from 'counter-store';

function YourController($scope: ng.IScope, counterStore: CounterStore) {
  counterStore.hook('INCREMENT_COUNT', (state) => {
    $scope.count = state.count;
  }).destroyOn($scope);
}

angular
  .module('App', [])
  .controller('YourController', YourController);
PreviousListen on state changes

Last updated 5 years ago

Was this helpful?