christopherthielen / ui-router-extras

THIS PROJECT IS NO LONGER MAINTAINED -- Extras for UI-Router for AngularJS. Sticky States (a.k.a. parallel states), Deep State Redirect (for tab-like navigation), Future States (async state definition)
http://christopherthielen.github.io/ui-router-extras/
MIT License
917 stars 211 forks source link

Not possible to pass Controller name as String to returned UI Router state in StateFactory #289

Closed andidev closed 8 years ago

andidev commented 8 years ago

I'm getting

Error: [ng:areq] Argument 'view1Controller' is not a function, got undefined
var requireCtrlStateFactory = function ($q, futureState) {
    var d = $q.defer();
    console.log("futureState = ", futureState);
    require([futureState.controller], function (lazyController) {
        var fullstate = {
            controller: futureState.controllerName, //== "view1Controller" controller name as String
            name: futureState.stateName,
            url: futureState.urlPrefix,
            templateUrl: futureState.templateUrl
        };
        d.resolve(fullstate);
    });
    return d.promise;
};
$futureStateProvider.stateFactory('requireCtrl', requireCtrlStateFactory);

The problem is that I create my controller with a module and therefore I can not return it which means that 'lazyController' will be undefined in the State Factory above.

define(['angular', 'app/view1/view1.module'], function (angular, view1Module) {
    "use strict";
    view1Module.controller('view1Controller', ['$scope', '$state', function ($scope, $state) {
        $scope.somedata = ['foo', 'bar', 'baz'];
    }]);
});

UI Router state supports setting controller by controller name with the controller field. How come this does not work when using a State Factory and Future States?

christopherthielen commented 8 years ago

That should work; I don't see why it doesn't. Are you sure the view1 module is successfully registering the controller?

Error: [ng:areq] Argument 'view1Controller' is not a function, got undefined This line with 'undefined' implies that view1Controller is not registered.

andidev commented 8 years ago

Thanks, I'll have to have to debug this then to have a closer look.

andidev commented 8 years ago

You are right. The module was not loaded. My requirejs skills are lacking. Thank you for your time.