ui-router icon indicating copy to clipboard operation
ui-router copied to clipboard

controllerProvider not $injecting resolve:{} parameters

Open Nthalk opened this issue 12 years ago • 11 comments

I understand that the templateProvider has a similar issue, here's the test case:

$stateProvider.state({
    url: "/",
    controllerProvider: function(resolveParam){},
    resolve: {resolveParam: function(){ return true;}}
});

Unknown provider: routeParamProvider <- routeParam

Nthalk avatar Jun 05 '14 22:06 Nthalk

I think controllerProvider just works with $stateParams as it is meant to just return a name of the controller. For resolve dependency injection use just controller.

$stateProvider.state({
    url: "/",
    controller: function(resolveParam){},
    resolve: {resolveParam: function(){ return true;}}
});

tylkomat avatar Jun 06 '14 09:06 tylkomat

I understand how it work now. Perhaps I should have marked this as a feature enhancement.

If I specify different controllers, the odds are that they might deal with different models, or require a model to determine the controller name.

I thought it was nice to have the routing layer resolve those dependencies, then decide a controller, optionally with those dependencies, and then feed those into the controller.

Nthalk avatar Jun 06 '14 14:06 Nthalk

I don't understand what you want to achieve, Usually you decide based on url parameters which controller should handle the call. This you can do with controllerProvider then the resolved dependencies are injected into the specified controller.

angular.module('app').config(
    function($stateProvider) {
        $stateProvider.state('main', {
            url: '/:controller',
            templateUrl: 'views/main.html',
            controllerProvider: function($stateParams) {
                // decide on controller based on params
                return controllerName; // i.e. myController
            },
            resolve: {
                myDependency: function() {
                    // something to resolve
                }
            }
        };
    }
).controller('myController', function($scope, myDependency, otherDependency) {
    // do something
});

The good thing here is that otherDependency is still resolved in the controller, while it was not before resolved in the router. With this you can for example define another controller, that only depends on $scope and myDependency or only myDependency or not at all. The controllerProvider just decides on the name. What your controller then needs is up to the controller itself.

Another options is an intermediate controller, that decides what to do based on your resolved dependencies and then redirect via $state.go() to another state, with or without changing the URL.

tylkomat avatar Jun 06 '14 14:06 tylkomat

I have some data driven forms that load custom logic and templates (business requirement).

Currently, I derive the model in a base controller and then load it into the app with a transclude el with the dataform.controller and dataform.templateUrl.

The design works, but make sub state routing (queryparams) difficult.

When I found controllerProvider and templateProvider, I figured my problems were over, until I realized that they did not wait for the dataform model to load. So... Why not? On Jun 6, 2014 7:30 AM, "Matthias Tylkowski" [email protected] wrote:

I don't understand what you want to achieve, Usually you decide based on url parameters which controller should handle the call. This you can do with controllerProvider then the resolved dependencies are injected into the specified controller.

angular.module('app').config( function($stateProvider) { $stateProvider.state('main', { url: '/:controller', templateUrl: 'views/main.html', controllerProvider: function($stateParams) { // decide on controller based on params return controllerName; // i.e. myController }, resolve: { myDependency: function() { // something to resolve } } }; } ).controller('myController', function($scope, myDependency, otherDependency) { // do something });

The good thing here is that otherDependency is still resolved in the controller, while it was not before resolved in the router. With this you can for example define another controller, that only depends on $scope and myDependency or only myDependency or not at all. The controllerProvider just decides on the name. What your controller then needs is up to the controller itself.

— Reply to this email directly or view it on GitHub https://github.com/angular-ui/ui-router/issues/1131#issuecomment-45342668 .

Nthalk avatar Jun 06 '14 14:06 Nthalk

If you add the data to the scope you should be able to access it or create a service to hold that data.

tylkomat avatar Jun 06 '14 15:06 tylkomat

However I would have to pass the param I'd to the service, and then controllerProvider would have to allow the return of a promise, which it does not currently. On Jun 6, 2014 8:19 AM, "Matthias Tylkowski" [email protected] wrote:

If you add the data to the scope you should be able to access it or create a service to hold that data.

— Reply to this email directly or view it on GitHub https://github.com/angular-ui/ui-router/issues/1131#issuecomment-45348601 .

Nthalk avatar Jun 06 '14 15:06 Nthalk

+1

cjroth avatar Jul 14 '14 23:07 cjroth

+1

SelrahcD avatar Aug 14 '14 13:08 SelrahcD

+1

fvelosa avatar Aug 31 '14 16:08 fvelosa

+1 need this

nihique avatar Oct 23 '14 09:10 nihique

+1's have been noted. Locking this issue to prevent further noise.

nateabele avatar Oct 23 '14 22:10 nateabele