ControllerProvider in UI-router results in error

允我心安 提交于 2019-11-29 16:40:18

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!