Use injectect module's controller in route definition

32 views
Skip to first unread message

msc...@factory42.com

unread,
Nov 30, 2014, 4:51:07 AM11/30/14
to ang...@googlegroups.com
Hi,

I'm new to angular and I'm trying to modularlize my app.

My main module gets some other module and I want to use the controller of the injected module in my route definition.
Some simple example would be very helpful!

var app = angular.module('Contacting_App', ['LeadLookup']);

app.config(['$routeProvider', function($routeProvider) {
   $routeProvider.
       when('/main', 
        {controller: 'MainCtrl', 
        templateUrl: 'apex/f42_Contacting_Main'}
       ).
       when('/lead', 
        {module: 'LeadLookup',
        controller: 'LeadLkpCtrl', 
        templateUrl: 'apex/f42_Lead_Lookup'}
       ).
       otherwise(
        {redirectTo: '/main'}
       );
}]);
 

Eric Eslinger

unread,
Nov 30, 2014, 10:32:47 AM11/30/14
to ang...@googlegroups.com
Personally, I would suggest putting the definition of the /lead route in the LeadLookup module. If you're going to modularize, don't do it by half steps. This is what I do, so that way you can put the /module routes, tests, controllers and so forth all in the same directory and all you have to do is (a) include them in the compiled javascript and (b) declare LeadLookup as a dependency on your top-level app.

e

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Marco Schmit

unread,
Dec 1, 2014, 3:25:46 AM12/1/14
to ang...@googlegroups.com
Thanks for your answer. 
sounds good.

I gave it a try but its running into some error.
Heres what i changed:

LeadLpk Module:

var leadLkp = angular.module('LeadLookup', []);

leadLkp.config(['$routeProvider', function($routeProvider) {
    $routeProvider.
        when('/lead', 
    {controller: 'LeadLkpCtrl', 
        templateUrl: 'apex/f42_Lead_Lookup'}
        )
}]);



Main Module:

var app = angular.module('f42_Contacting_App', [LeadLookup', 'ngRoute']);

app.config(['$routeProvider', function($routeProvider) {
   $routeProvider.
       when('/main', 
        {controller: 'MainCtrl', 
        templateUrl: 'apex/f42_Contacting_Main'}
       ).
       /*when('/lead', 
        {controller: 'LeadLkpCtrl', 
        templateUrl: 'apex/f42_Lead_Lookup'}
       ).*/
       otherwise(
        {redirectTo: '/main'}
       );
}]);


Am I missing something?

Thanks!

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/brFTHDPalnU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.

To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.



--
Marco Schmit | 
email: msc...@factory42.com | fon: +49 89 878067657 | fax: +49 89 8780676 99

factory42 GmbH | Rosenheimer Strasse 145 b | 81671 München | Germany
Geschäftsführer: Michael Frohn | Unternehmenssitz: München | Amtsgericht München | HRB 198418
factory42
München | Berlin | Frankfurt | Düsseldorf
www.factory42.com

Innovationspreis IT - Best of 2013 - Branchenpreis    Salesforce Certified Administrator     Salesforce Certified Developer

Eric Eslinger

unread,
Dec 1, 2014, 8:14:53 AM12/1/14
to ang...@googlegroups.com
You also have to do leadLkp.controller('LeadCtrl', LeadCtrl) or something similar where you introduce the controller itself to the dependency injection system.

E

Marco Schmit

unread,
Dec 1, 2014, 9:39:40 AM12/1/14
to ang...@googlegroups.com
Yes sure I did that too:

leadLkp.controller('LeadLkpCtrl', [......, function(...{ ...

I'm going with the code from the beginning for now ... there was only some syntax failure elsewhere that caused the code not to work.

Thank you!
Reply all
Reply to author
Forward
0 new messages