本文介绍了ControllerProvider在错误UI路由器结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UI路由器 StateProvider ,需要控制器和放大器之间挑;基于外部数据的看法,所以我用 TemplateProvider ControllerProvider

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

如果我只用了 TemplateProvider 这一切工作正常,但是当我添加 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)

看起来有些未定义 服务名可能(它说了对象,但它看起来像它的未定义但我不是在调试巨大的 AngularJs

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)

下面是我的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();
        }]
    }})

修改

根据拉迪姆Köhlers答案更改

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');
                }
            })
        }]
        })

新的错误:

    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

这至少需要版本 0.2.14 角-UI-router.js

推荐答案

的一点是,该 controllerProvider 只是必须返回的目标重新presenting的控制器字符串(它的名字)

The point is, that controllerProvider simply must return object representing the controller or string (its name)

的.$stateProvider

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

stateConfig.controllerProvider (可选)的功能

这是返回实际的控制器或注射提供商功能的字符串

Injectable provider function that returns the actual controller or string.

所以,我们不能回到诺言...

So, we cannot return promise...

不过,有一个解决方案:

But there is a solution:

我们可以使用内置功能的解析 - 做异步的东西,使 controllerProvider 英雄 - 通过消耗:

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.

controllerProvider (一次全部解决)只是需要这一结果,并返回一个字符串。

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

检查一下

这篇关于ControllerProvider在错误UI路由器结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 03:35