Modifying your Application State by Dispatching Actions
Most Redux apps have a set of functions, called "action creators", that are used to set up and dispatch actions.
In Angular, it's convenient to define your action creators as @Injectable() services, decoupling the dispatch, creation and side-effect logic from the @Component classes in your application.
As you can see, the action creators are simple functions that dispatch Action objects containing more information that describes the state modification.
Asynchronous Actions
This "ActionCreatorService" pattern comes in handy if you must handle asynchronous or conditional actions (users of react-redux may recognize this pattern as analogous to redux-thunk in a dependency-injected world).
In the incrementIfOdd() action creator, we create a one-time subscription to the counter's currentValue in the application state. From there, we check to see if it's odd before dispatching an action.
In the incrementAsync() action creator, we are delaying the actual call to dispatch(). We created a Promise that will resolve after the delay. Once the Promise resolves, we can then dispatch an action to increment the counter.
In more complex instances, you may however find this to be cumbersome, this is where we recommend utilizing ngrx's effect pattern, which we talk more about here.
Actions that Depend on Other Services
The ActionCreatorService pattern becomes necessary in cases where your action creators must use other Angular services. Consider the following SessionActions service that handles a remote API call: