UI路由器决心抛出错误提供商未找到路由器、抛出、提供商、未找到

2023-09-13 05:01:06 作者:哀家笑看各种狗

我有一个UI -router状态此处

I have got a ui -router state here

 var AccountParentState = {
            url: "/Account",
            views: accountrootview,
            stickA: true,
        },
            AccountAddState = {
                url: "/add",
                views: addaccountview,
                controller: "account-controller",
                resolve: {
                    Name: function () {
                        return "Joydeep";
                    }
                }
            };

        $stateProvider
            .state('account', AccountParentState)
            .state("account.add", AccountAddState)

这是我的控制器:

And this is my controller :

angular.module('ngApp').controller('account-controller', ['$scope', '$state', "account", "plugin-factory", "Name", function ($scope
    , $state
    , account
    , plugins, Name) {

    console.log(Name);


}]);

当我试图解析名称帐户控制器内。它引发错误的:

When I am trying to resolve the Name within the account-controller . Its throwing the error as :

Unknown provider: NameProvider <- Name <- account-controller

我的问题是如何解决这种情况。或解决使用决心属性UI路由器状态中的数据。

My question is how to resolve this situation . Or resolve the data within the ui-router state using resolve property .

推荐答案

要帮助你看到的问题,我的创建工作plunker ,这确实使用类似的定义上面的状态:

To help you to see the issue, I created working plunker, which does use similar definition to the above states:

  var AccountParentState = {
    url: "/Account",
    views: { '' : 
       { template: 'Account state <hr/><div ui-view=""></div>',} },
    stickA: true, 
  },
    AccountAddState = {
      url: "/add",
      //views: addaccountview,
      templateUrl: 'tpl.html',
      controller: "account-controller",
      resolve: {
        Name: function() {
          return "Joydeep";
        }
      }
    };

  $stateProvider
    .state('account', AccountParentState)
    .state("account.add", AccountAddState)

工厂及控制器:

  .factory('account', function(){return {}})
  .factory('plugin-factory', function(){return {}})
  .controller('account-controller', 
     ['$scope', '$state', "account", "plugin-factory", "Name"
     , function($scope, $state, account, plugins, Name) {    
       console.log(Name);
     }
  ])

这是工作,因为控制器是由 UI-路由器实例,作为国家转型的一部分​​。

And that is working, because controller was instantiated by the UI-Router, as a part of the state transition.

如果,在另一方面,我们将扩大这个那个例子(见分叉破碎的版本)中的index.html线(国家DEF外)

If, on the other hand, we will extend that example with this (see forked broken version) line in the index.html (outside of the state def)

<div ng-controller="account-controller"></div>
<div ui-view=""></div>

问题就会出现。为什么?因为现在控制器的不可以由管理​​UI-路由器。它不是由它的基础设施,不具备配置价值注入名称。它是纯角的风格..

The issue will appear. Why? Because now the controller is not managed by the UI-Router. It is not injected by its infrastructure, not provided with configured value 'Name'. It is "plain angular" style..

检查工作液这里和破碎的这里