Registering Controller with new function $controllerProvider.register()

5,460 views
Skip to first unread message

Sudsy

unread,
Apr 4, 2012, 3:23:02 AM4/4/12
to ang...@googlegroups.com
Hi There,

I am trying to separate my controllers into separate requirejs module files to assist with maintainability of my project.

I have got most of it working except for the controllers. I noticed that there is a $controllerProvider service that has a register function to allow the registration of modules and I have been trying this method (code below) but I can't seem to get it to work.

At the end of the day I am looking for the cleanest way to have one controller per file and then each controller loaded by requirejs so if there is a much better way than I am attempting please let me know.

Each Controller file looks something like this

define([
    "services/dependency"
], function () {

    "use strict";

    var controller = function ($scope, servicedependency) {
        $scope.search = function () {
            // some sort of search function here
        }

    };

    controller.$inject = ['$scope', 'servicedependency'];


    var $injector = angular.injector();
    $injector.invoke(['$controllerProvider', function ($controllerProvider) {
        // Register The Controller
        $controllerProvider.register('MyCtrl', controller);

    }]);


    return controller;

});

This code gives me "unknown provider $controllerProviderProvider <- $controllerProvider" I get a similar error when trying to use $controller as an injected service instead.

Any suggestions?

Ben



Peter Bacon Darwin

unread,
Apr 4, 2012, 3:36:29 AM4/4/12
to ang...@googlegroups.com
Just guessing here and not able to check but I think that XxxxProvider objects should only be accessed during the config phase of the module.  I.E. You would have angular.module("MyModule",[]).config(function($controllerProvider){...});
One possible method is to have a single global namespace object, e.g. window.MyApp and then attach all your controllers to that.
I believe that there are plans to attach controllers to modules in the same way you can do so with services and directives but that this is going to be post 1.0.
Pete





--
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/-/LFL3EumbVmEJ.
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.

Sudsy

unread,
Apr 4, 2012, 4:28:06 AM4/4/12
to ang...@googlegroups.com
Thanks,

Using the global object was an easy way around it.

Ben


On Wednesday, April 4, 2012 5:36:29 PM UTC+10, Peter Bacon Darwin wrote:
Just guessing here and not able to check but I think that XxxxProvider objects should only be accessed during the config phase of the module.  I.E. You would have angular.module("MyModule",[]).config(function($controllerProvider){...});
One possible method is to have a single global namespace object, e.g. window.MyApp and then attach all your controllers to that.
I believe that there are plans to attach controllers to modules in the same way you can do so with services and directives but that this is going to be post 1.0.
Pete

To unsubscribe from this group, send email to angular+unsubscribe@googlegroups.com.

Igor Minar

unread,
Apr 4, 2012, 11:25:12 AM4/4/12
to ang...@googlegroups.com
On Wed, Apr 4, 2012 at 12:36 AM, Peter Bacon Darwin <pe...@bacondarwin.com> wrote:
Just guessing here and not able to check but I think that XxxxProvider objects should only be accessed during the config phase of the module.  I.E. You would have angular.module("MyModule",[]).config(function($controllerProvider){...});
One possible method is to have a single global namespace object, e.g. window.MyApp and then attach all your controllers to that.

why not just use requirejs for this? in the main file of the app create the module and export it with requirejs and the import it in all the other files where you can then do something like myApp.factory(..) or myApp.controller(...);
 
I believe that there are plans to attach controllers to modules in the same way you can do so with services and directives but that this is going to be post 1.0.

this was already done in rc3, myModule.controller('FooCtrl', fn);

/i

Peter Bacon Darwin

unread,
Apr 4, 2012, 11:27:56 AM4/4/12
to ang...@googlegroups.com
Oh nice!  I missed that one.  So does that mean that the compiler will pick up the FooCtrl out of the ng-app module?

Igor Minar

unread,
Apr 4, 2012, 12:13:19 PM4/4/12
to ang...@googlegroups.com
yes. the controller resolution is documented here: http://docs-next.angularjs.org/api/angular.module.ng.$controller

Ankit Rao

unread,
Jul 17, 2019, 1:01:02 PM7/17/19
to Angular and AngularJS discussion
I'm facing the same problem , with dynamic injection of controller, function is not defined, 
And $controllerProvider is not getting injected


On Wednesday, April 4, 2012 at 9:43:19 PM UTC+5:30, Igor Minar wrote:
yes. the controller resolution is documented here: http://docs-next.angularjs.org/api/angular.module.ng.$controller

On Wed, Apr 4, 2012 at 8:27 AM, Peter Bacon Darwin <pe...@bacondarwin.com> wrote:
Oh nice!  I missed that one.  So does that mean that the compiler will pick up the FooCtrl out of the ng-app module?


On 4 April 2012 16:25, Igor Minar <ig...@angularjs.org> wrote:

I believe that there are plans to attach controllers to modules in the same way you can do so with services and directives but that this is going to be post 1.0.

this was already done in rc3, myModule.controller('FooCtrl', fn);

/i

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to ang...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages