Building an app in Angular involves consuming various APIs and providing options for users to select, which are then recorded and sent back to the server.
The design of the app is structured as follows:
- All common logic is placed in the Main Controller, with other options separated into different controllers that act as children of the main controller.
- The Main Controller retrieves all necessary data required to run the app, which is then accessed by all child controllers.
- To ensure that the data is loaded before execution, promises are attached to the scope so that child controllers are aware when the data is available.
- Data updating functionalities within child controllers have been moved to the main controller to consolidate updates that occur on a single object.
- Communication between child and main controllers is facilitated through emitting/broadcasting events. When an update occurs in a child controller, it emits an event with data that is captured and processed by the Main Controller for updating.
MainController { $scope.loaded = DataService.get(); $scope.userOptions = {}; $scope.$on('update',function(){ updateUserOptions(); }) } ChildController { $scope.loaded.then(function(){ //all logic of child controller } $scope.onselect = function(){ $scope.$emit('update',data); } }
Some questions to consider:
- Is utilizing events between controllers considered good practice?
- Is attaching promises to scope beneficial for child controllers?
- Would incorporating services improve the code structure?