javascript - ControllerProvider in UI-router results in error -


i have ui-router stateprovider , need pick between controllers & views based on external data, used templateprovider , controllerprovider.

if had templateprovider works fine, when add controllerprovider error:

error: [ng:areq] argument 'fn' not function, got object http://errors.angularjs.org/1.3.1/ng/areq?p0=fn&p1=not%20ananunction%2c%20got%object     @ regex_string_regexp (http://localhost:48510/scripts/vendor/angular/1-angular.js:80:12)     @ assertarg (http://localhost:48510/scripts/vendor/angular/1-angular.js:1577:11)     @ assertargfn (http://localhost:48510/scripts/vendor/angular/1-angular.js:1587:3)     @ annotate (http://localhost:48510/scripts/vendor/angular/1-angular.js:3417:5)     @ invoke (http://localhost:48510/scripts/vendor/angular/1-angular.js:4096:21)     @ object.instantiate (http://localhost:48510/scripts/vendor/angular/1-angular.js:4129:23)     @ http://localhost:48510/scripts/vendor/angular/1-angular.js:8320:28     @ compile (http://localhost:48510/scripts/vendor/angularuirouter/angular-ui-router.js:3897:28)     @ invokelinkfn (http://localhost:48510/scripts/vendor/angular/1-angular.js:8081:9)     @ nodelinkfn (http://localhost:48510/scripts/vendor/angular/1-angular.js:7593:11) 

looks undefined servicename possibly (it says got object, looks it's undefined i'm not great @ debugging angularjs)

here code:

.state('abstractparent.childstate', {     url: '/childstate',     controllerprovider: ['myservice', function (myservice) {         return myservice           .isspecificmember()           .then(function (result) {  //this returns promise of bool.             if (result.data) {                 return 'specificcontroller';             } else {                 return 'generalcontroller';             }         })     }],     templateprovider: ['myservice', '$templatefactory',      function (myservice, $templatefactory) {         return myservice.isspecificmember().then(function(result) {             if(result.data)             {                 return $templatefactory.fromurl('specificview');             } else {                 return $templatefactory.fromurl('genericview');             }         })     }],     resolve: {         thing: ['thingservice', function (thingservice) {              //this used in 1 of controllers.             return thingservice.getthing();         }]     }}) 

edit

changes based on radim köhlers answer

    .state('abstractparent.childstate', {         url: '/childstate',         resolve: {             controllername: ['myservice', function (myservice) {                 return myservice.isspecificmember().then(function (result) {  //this returns promise of bool.                     if (result.data) {                         return 'specificcontroller';                     } else {                         return 'generalcontroller';                     }                 })             }],             thing: ['thingservice', function (thingservice) { //this used in 1 of controllers.                 return thingservice.getthing();             }]         },         controllerprovider: ['controllername', function (controllername) {             return controllername;         }],         templateprovider: ['myservice', '$templatefactory', function (myservice, $templatefactory) {             return myservice.isspecificmember().then(function(result) {                 if(result.data)                 {                     return $templatefactory.fromurl('specificview');                 } else {                     return $templatefactory.fromurl('genericview');                 }             })         }]         }) 

new error:

    error: [$injector:unpr] unknown provider: controllernameprovider <- controllername     http://errors.angularjs.org/1.3.1/$injector/unpr?p0=controllernameprovider%20%3c-%20controllername         @ regex_string_regexp (1-angular.js:80)         @ 1-angular.js:3930         @ object.getservice [as get] (1-angular.js:4077)         @ 1-angular.js:3935         @ getservice (1-angular.js:4077)         @ object.invoke (1-angular.js:4109)         @ angular-ui-router.js:3465         @ processqueue (1-angular.js:12901)         @ 1-angular.js:12917         @ scope.$get.scope.$eval (1-angular.js:14110) 

solution

this needed @ least version 0.2.14 of angular-ui-router.js

the point is, controllerprovider must return object representing controller or string (its name)

http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$stateprovider

stateconfig.controllerprovider (optional) function

injectable provider function returns actual controller or string.

so, cannot return promise...

but there solution:

there a working example

we can use built-in feature resolve - async stuff, , make controllerprovider hero - consuming it:

  resolve: {     controllername: ['$stateparams', '$timeout','$q',          function ($stateparams, $timeout, $q)         {           var deferred = $q.defer();            $timeout(function(){               deferred.resolve('mylazyrevealedctrl');            },250);           return deferred.promise;         }],    },   controllerprovider:['controllername', function (controllername)   {       return controllername;   }], 

what can see simplified example timer (playing role of async .then()). waits while , returns resolved controller name.

the controllerprovider (once resolved) takes result, , returns string.

check here


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -