Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
683 views
in Technique[技术] by (71.8m points)

javascript - ControllerProvider in UI-router results in error

I have a ui-router StateProvider and need to pick between controllers & views based on external data, so I used TemplateProvider and ControllerProvider.

If I only had the TemplateProvider it all works fine, but when I add the ControllerProvider I get this error:

Error: [ng:areq] Argument 'fn' is not a function, got Object
http://errors.angularjs.org/1.3.1/ng/areq?p0=fn&p1=not%20aNaNunction%2C%20got%Object
    at REGEX_STRING_REGEXP (http://localhost:48510/Scripts/Vendor/Angular/1-angular.js:80:12)
    at assertArg (http://localhost:48510/Scripts/Vendor/Angular/1-angular.js:1577:11)
    at assertArgFn (http://localhost:48510/Scripts/Vendor/Angular/1-angular.js:1587:3)
    at annotate (http://localhost:48510/Scripts/Vendor/Angular/1-angular.js:3417:5)
    at invoke (http://localhost:48510/Scripts/Vendor/Angular/1-angular.js:4096:21)
    at Object.instantiate (http://localhost:48510/Scripts/Vendor/Angular/1-angular.js:4129:23)
    at http://localhost:48510/Scripts/Vendor/Angular/1-angular.js:8320:28
    at compile (http://localhost:48510/Scripts/Vendor/AngularUIRouter/angular-ui-router.js:3897:28)
    at invokeLinkFn (http://localhost:48510/Scripts/Vendor/Angular/1-angular.js:8081:9)
    at nodeLinkFn (http://localhost:48510/Scripts/Vendor/Angular/1-angular.js:7593:11)

Looks like some undefined serviceName possibly (it says got object, but it looks like it's undefined but I'm not great at debugging AngularJs)

Here is my code:

.state('abstractParent.childState', {
    url: '/childState',
    controllerProvider: ['myService', function (myService) {
        return myService
          .isSpecificMember()
          .then(function (result) {  //this returns a promise of a 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 is only used in one of the 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 a promise of a bool.
                    if (result.data) {
                        return 'SpecificController';
                    } else {
                        return 'GeneralController';
                    }
                })
            }],
            thing: ['thingService', function (thingService) { //this is only used in one of the 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
        at REGEX_STRING_REGEXP (1-angular.js:80)
        at 1-angular.js:3930
        at Object.getService [as get] (1-angular.js:4077)
        at 1-angular.js:3935
        at getService (1-angular.js:4077)
        at Object.invoke (1-angular.js:4109)
        at angular-ui-router.js:3465
        at processQueue (1-angular.js:12901)
        at 1-angular.js:12917
        at Scope.$get.Scope.$eval (1-angular.js:14110)

SOLUTION

This needed at least version 0.2.14 of angular-ui-router.js

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The point is, that controllerProvider simply must return object representing the 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 that returns the actual controller or string.

So, we cannot return promise...

But there is a solution:

There is a working example

We can use built-in feature resolve - to do the async stuff, and make controllerProvider a hero - by 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 we can see is simplified example with just a timer (playing role of async .then()). It waits a while and returns resolved controller name.

The controllerProvider then (once all is resolved) just takes that result, and returns it as a string.

Check it here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...