Communication between a view, controller, and directive using events
One of the most confusing aspects of using Ionic is when and how to leverage eventing for communication between various components. Let's take a look at the various scenarios and different ways to handle the data flow:
- State to view and/or controller: This is handled by UI-Router. There are several options. You can use the
resolve
object declared at the route level and pass it to the controller just as a factory. Another option is to pass data as a parameter in$stateParams
so that the controller for that specific state can receive data. - View to view: To share data in different views, store them in a parent scope,
$stateParams
, or factory. If it's just a simple piece of data, mainly for display purposes, it's OK to use the parent scope method. However, if the data is specific to state, it should be a part of the state parameters. Factory is always a good practice to keep the shared data between state, view...