Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use nested controllers instead of services in angularjs?

I just started to use AngularJS, so I'm not an expert.

I have a div that represent the right area of my html view. In that div I have a controller, i.e.

<div class="rightContainer" ng-controller="rightContainerCtrl">...</div>

Inside that div I have a table, a search region, etc. Each region inside that div has its own controllers, it looks like this:

<div class="rightContainer" ng-controller="rightContainerCtrl">
...
   <div class="search" ng-controller="searchCtrl">...</div>
...
   <div class="table" ng-controller="tableCtrl">...</div>

 </div>

the search region for example has its own controller and it is a child of rightContainerCtrl because it needs to alter some content in the parent (rightContainerCtrl), but the rightContainer div is growing and now it's big, and contain several nested controllers.

I think that using this nested controllers it's bad in this context because all the nested controllers share the parent scope, and not all controllers needs to access all the parent scope variables, also all the controllers are "prisoners" of the rightContainerCtrl, so they are highly coupled with their parent controller.

It looks like a God object anti-pattern (God controller in this case), so I think that instead of using nested controllers I can refactor my code to eliminate the rightContainerCtrl controller and use a service instead (like in a facade design pattern), that service then will be used by the controllers instead of sharing scope variables.

but since I'm not an AngularJs expert I'm not sure if I'm right or if it's better to leave this parent controller, maybe I'm missing something, so my question is

When is better to use nested controllers (nested scopes) and when it's better to use services instead in angularjs?

like image 790
Miguel A. Carrasco Avatar asked Jul 22 '13 17:07

Miguel A. Carrasco


People also ask

Can we use nested controller in AngularJS?

Nested Controllers: AngularJS allows using nested controllers. It means that you have specified a controller in an HTML element which is a child of another HTML element using another controller.

Can we put nested controllers in an angular application?

You can use either of the two. But, Controller as Syntax makes your code more readable as we have seen earlier in Nested Scopes. If you want to use $scope, it has to be injected into Controller function.

What is the use of angular controllers in the application?

All the AngularJS application mainly relies on the controllers to control the flow of data in that application. Basically, it controls the data of AngularJS applications and the controller is a Javascript object, created by a standard JavaScript object constructor.

What is the use of controller in AngularJS?

AngularJS controllers are used to control the flow of data of AngularJS application. A controller is defined using ng-controller directive. A controller is a JavaScript object containing attributes/properties and functions.


2 Answers

Controller/scope hierarchy should not dictate how data/models are shared in an application. When you think of data sharing in Angular, think dependency injection.

In the video that is referenced in @shaunhusain's answser, Misko states that scope should refer to the model, not be the model -- so don't model/put your data into scopes. Your models/data should normally be in a service.

When writing an Angular app, first think about your models. Put them in services with APIs to get/edit/manipulate the models. Then design your views. Each view should project/use/manipulate some subset of your models. Define a controller for each view that simply glues the needed subset of the models to the view. Make your controllers as thin as possible.

(Naming a controller rightContainerCtrl is also not recommended. The controller should not know about presentation/layout.)

like image 115
Mark Rajcok Avatar answered Oct 05 '22 17:10

Mark Rajcok


This is 100% judgement call, and should be based on a couple of points.

Using events creates extremely loosely coupled components, they literally do not need to be aware of one another, if you have a situation where some parent controller would alleviate the need to communicate between a bunch of controllers (via services) then it is probably a better solution.

However if you're okay with the controllers each depending on the service (not really a problem) then you could just use the service as a means of communicating changes between the controllers. I've seen tons of arguments against the singleton (of which the service is a flavor, an injected singleton, but singleton nonetheless) I find these arguments to mostly be moot and generally lack a truly elegant and concise solution. If an argument spouts on and on about how when you go from A - D you don't want to take road B but they never seem to offer road C I don't really see the point.

http://www.youtube.com/watch?v=ZhfUv0spHCY&t=30m34s

I couldn't find the exact point in the video, but somewhere at the end here he discusses the usage of controllers vs services (he also reviews bi-directional data binding which will stop you from needing to pollute the global scope so to speak).

like image 24
shaunhusain Avatar answered Oct 05 '22 16:10

shaunhusain