ui router resolve is throwing error provider not found

做~自己de王妃 提交于 2019-12-10 21:53:31

问题


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

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


回答1:


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)

Factories and controller:

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

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

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>

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..

Check the working solution here and the broken here



来源:https://stackoverflow.com/questions/28712778/ui-router-resolve-is-throwing-error-provider-not-found

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