30

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.

  • All the common logic in Main Controller and all other options in different controllers as the child of main controller.
  • Main Controller retrieve all the data that are required to run the app. which is consumed by all other child controllers.
  • To make sure data is loaded I am using promise attached to scope. So all the child controller will know data loaded.
  • I have moved data updation part of all child controllers to main controller because all the updates happen in one object.
  • Child Controller emit/broadcast to communicate between child and main. So when update happens child will emit an event with data which will be captured by Main and it will do the update.
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

  1. Is it a good practice to use events between controllers ?
  2. is it good to use promise attached to scope for child controllers ?
  3. Will it improve my code if I start using services ?
Sreevisakh
  • 1,896
  • 2
  • 16
  • 23
  • 1
    What is your primary idea of using a Main Controller with relation to Child Controllers - in other words what architectural problem you are trying to solve with it? – forsberg Aug 01 '15 at 05:45
  • 1
    They all need the same data. If I update my Main all child will get the updated data. I can control all childs using my main controller using variables attached to main scope. – Sreevisakh Aug 01 '15 at 05:49
  • 2
    are you talking about pub/sub pattern? – Robert Koritnik Aug 04 '15 at 12:13

6 Answers6

18

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:

  1. Is it a good practice to use events between controllers? IMHO, it is the most flexible way to share information between all controllers even if they have isolated scope (using $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 $scopes inherit from $rootScope.
  2. is it good to use promise attached to scope for child controllers ? Firstly, you have to learn about how scope inheritance works. You have to take care to avoid property shadow in JS. Secondly, I would move out all the logic from 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.
  3. Will it improve my code if I start using services ? I answered this question above.

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:

  1. To make sure data is loaded I am using promise attached to scope. So all the child controller will know data loaded.. Instead I would emit a custom 'loaded' event in the MainController using $scope.$emit('loaded'). After that, in the ChildController I would use $scope.$on('loaded', function(){}) to handle the event.
  2. I would move the updateUserOptions function to a service and inject the it into just the controllers that need it.

I hope that helps!

Andrew Kuklewicz
  • 10,621
  • 1
  • 34
  • 42
FedeF
  • 457
  • 3
  • 9
8

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.

Ori Drori
  • 183,571
  • 29
  • 224
  • 209
7
  1. Is it a good practice to use events between controllers ?

No it's not, it will be deprecated by Angular JS 2.0. It also often leads to unmanagable tangle of events which are hard to understand and debug. Use services to share data between controllers. (Inject same service into multiple controllers, service then holds data, controllers bind to that data and are automatically synchronized) I wrote a blog post explaining this use case.

  1. Is it good to use promise attached to scope for child controllers ?

No it's not. Use promises and resolve data in services. Don't use $scope at all but use controllerAs syntax instead. $scope was deprecated also in Angular JS 1.X because it's usage leads to many different problems with scope inheritance.

  1. Will it improve my code if I start using services ?

YES! Use services for all logic and data manipulation. Use controllers only for UI interaction and delegate everything to services. Also use ui-router for managing state of your application.

tomastrajan
  • 1,728
  • 14
  • 28
5

I'm not going to answer your questions directly as I have some other comments as well. I think the approach you mentioned is not the best way to build angular applications.

  1. All the common logic in Main Controller and all other options in different controllers as the child of main controller.

It's against all angular style guides to place common logic in controllers. Controllers should only be used for the logic related to the view (data binding, validation, ...). Because the code inside a controller is not reusable, the less code you have in a controller the better. The more logic you have in services, the more scalable your application becomes.

Fix: I suggest you create a service that retrieves data from the server, and inject this service in controllers as you need. Notice also this way offers better dependency management as you can keep track of which controllers need which services exactly.

  1. Nested controllers should be avoided when possible, because angular keeps track of all the active scopes and re-evaluates them in every $apply() loop.

Fix: same as #1, use services instead of the main controller.

  1. To make sure data is loaded I am using promise attached to scope. So all the child controller will know data loaded.

Using a promise for data retrieval is a good practice. But, again, keeping it in a service is much cleaner than main controller.

  1. I have moved data updation part of all child controllers to main controller because all the updates happen in one object.

Child Controller emit/broadcast to communicate between child and main. So when update happens child will emit an event with data which will be captured by Main and it will do the update.

Fix: use a service with an update function instead of events. Events are harder to debug and track. And you need to unregister event handlers on destroying a controller. If you can use a function/promise instead of events, then it's usually a better idea.

gafi
  • 12,113
  • 2
  • 30
  • 32
  • But how my other controllers will know if the data in the service is updated? – Sreevisakh Aug 11 '15 at 14:42
  • You can use a promise if the data is only retrieved once. Otherwise I guess it's ok to use events or callbacks – gafi Aug 13 '15 at 09:08
  • I recently saw this - If I have service DataService, I will define a variable in that and attach that to all the controllers in the scope. My update function will update this service object which intern will update scope. how about this? – Sreevisakh Aug 13 '15 at 10:14
  • that might work, but it will be very hard to debug as the object is changing outside of all controllers – gafi Aug 13 '15 at 18:36
5

Is it a good practice to use events between controllers ?

A problem with your current set-up is that you're implicitly relying on the hierarchy of your controllers (the fact that one is the child of the other) - because you emit the event, only scopes higher up on the hierarchy can catch it. Besides being an implicit connection (that a developer has to remember), this also limits he extendability of this feature.

On the other hand, if you injected a shared service into all the controllers that need it, the connection between the controllers would become explicit and documented, and their scopes' position in the hierarchy independent. This will make your architecture easier to maintain, with the added benefit of also being easier to test, for one.

You can still implement an observer pattern with a service.

is it good to use promise attached to scope for child controllers ?

The issue of polluting scopes pointed out in other answers is valid. This is one of the reasons why it's better to limit the number of objects you attach to your scope, and to use objects as bundles of variables on your scope instead of attaching all the variables to the scope directly. (For an explanation of these reasons, see discussions about "always having a . in your bindings".)

(Of course, don't do this blindly just to reduce the number of variables, try to find semantic connections between variables that might be bundled together sensefully.)

Will it improve my code if I start using services ?

I think the above answers already outline the answer for this: yes. There are other benefits too, but this format is not best for too long answers, so I won't list anything else now.

All in all, these above pointers are not big issues with your code currently, but if you're looking for the best architecture, I think you can do better.

Community
  • 1
  • 1
doldt
  • 4,466
  • 3
  • 21
  • 36
2

Answers:

  1. No, it will be deprecated soon.

  2. $scope is deprecated already.

  3. Services is a great choice. Services allow us to share data and behaviour across other objects like controllers.