Disambiguating when two modules define a controller with the same name?

987 views
Skip to first unread message

Josh Mandel

unread,
May 1, 2012, 2:30:25 PM5/1/12
to ang...@googlegroups.com
Here's an example where a `myApp` module requires two submodules, each of which defines a controller called `MainController`:

Can my view explicitly specify which controller to use?  Or is the behavior fully determined by the order of dependencies at module registration:
myApp angular.module('myApp'['ngSanitize''realModule''fakeModule']);

Thanks!

Brendan Owen

unread,
May 1, 2012, 6:24:33 PM5/1/12
to ang...@googlegroups.com
I have seen examples where the controller, service, etc have a name space defined by the module name. For example

fakeModule angular.module('fakeModule'[]);
realModule angular.module('realModule'[]);

realModule.controller("realModule.MainCtl"function($scope{
    $scope.name '<b>Real controller</b>';
});


fakeModule.controller("fakeModule.MainCtl"function($scope{
    $scope.name '<i>Fake controller</i>';
});


myApp angular.module('myApp'['ngSanitize''realModule''fakeModule']);


<div ng-app="myApp" ng-controller="realModule.MainCtl">
   Text:  {{name}}<br><br>
   Safe Html:  <div ng-bind-html="name"></div>   
   Raw Html:  <div ng-bind-html-unsafe="name"></div>   
</div>

jma...@gmail.com

unread,
May 1, 2012, 8:27:06 PM5/1/12
to ang...@googlegroups.com
Never allowing two modules to define a controller with the same name works fine as a convention  -- but are there other approaches?  (e.g. what if I'm re-using 3rd party modules that don't adopt this naming convention?)



Marc Chung

unread,
May 1, 2012, 8:55:31 PM5/1/12
to ang...@googlegroups.com
That's a good question. Maybe a naming convention could be applied.

Something like Java packages:

myApp = angular.module('com.marcchung.myApp', ['ngSanitize',
'realModule', 'fakeModule']);

Brings up another question:

Is there a story around 3rd party modules? Sharing? Project
conventions? Has anything like this surfaced yet?

- Marc
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/angular/-/1OesXhxju60J.
>
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/angular?hl=en.
Reply all
Reply to author
Forward
0 new messages