I am building an app in angular, which consumes different APIs and Gives options for the user to select it will be recorded and sent back to the server.
I have designed it as follows.
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); } }
Questions
Angular is a platform or framework to build client-based applications in HTML and TypeScript. It is written in TypeScript. It implements core and optional functionality as a set of TypeScript libraries that are imported into applications. Angular consists of Three main things that are Modules, Components, and Routing.
The core of the Angular framework architecture is Angular Component. Angular Component is the building block of every Angular application. Every angular application is made up of one more Angular Component. It is basically a plain JavaScript / Typescript class along with a HTML template and an associated name.
Angular follows component based architecture, in component-based architecture, a large application is broken (decoupled) into functional and logical components. These components are reusable hence can be used in any other part of the application.
I will try to answer your question based on my own experience. Recently I've built a single page application and I've refactored its architecture.
Here are my answers:
$broadcast
or $emit
for example). This is called the Observer design pattern. However, you can use services instead of events to share data between them. If you are going to use $rootScope
, be careful as all the $scope
s inherit from $rootScope
.scope.loaded
in ChildController
to a service such as ChildService
. Keeping the business logic (such as request, etc) in Services instead of Controllers, will ensure it can be re-used. Segregation of business logic is good design principle.In addition, in order to build a good architecture I've read this angular style guide written by John Papa.
I recommend the following changes:
$scope.$emit('loaded')
. After that, in the ChildController I would use $scope.$on('loaded', function(){})
to handle the event.updateUserOptions
function to a service and inject the it into just the controllers that need it.I hope that helps!
Is it a good practice to use events between controllers ? Not as the main form of data sharing, but you can use it to notify about system events, such as data ready.
Is it good to use promise attached to scope for child controllers ? Don't use scope inheritance, it causes lots of annoying problems.
Will it improve my code if I start using services ? Yep.
This is what I would do in your place:
dataService - this service is responsible for all data coming in / going out. Whenever a request for data is made (no matter which controller asked for the data), the service caches the data (save the promise is good enough). All further requests get the cached data unless they specify they want a fresh data. Whenever data is updated (1st time or refresh), the service broadcasts a 'dataReady' event via $rootScope to which the main controller and other subscribers can listen. The service is also responsible for data updates, and when the data is updated you can also broadcast an event via the $rootScope. When the event is activated, all subscribers query the service, and get the data they need.
Controllers - avoid controllers, use directives with isolated scope, and pass the data between them using attributes. In this way you can be sure that each directive gets what it needs, and not everything. The directives can communicate using attributes, services, broadcast / emit or require their parents / siblings if they work closely together.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With