角"未知提供商QUOT; - 如何使用routeProvider配置中的工厂? [英] Angular "Unkown Provider" - how to use a factory within routeProvider configuration?

查看:246
本文介绍了角"未知提供商QUOT; - 如何使用routeProvider配置中的工厂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在玩弄角我试图更好地了解如何使用厂,服务,常量,路由等核心概念。所以我建立与节点,前preSS,玉器和角一个简单的演示应用程序。

现在我想用routeProvider里面的配置价值。我创建了一个恒定的,这工作正常。为了使以备将来使用更灵活我建一个工厂的话,但这种失败,未知供应商。这是角实例序列的问题吗?我为什么不能注入出厂到的.config 部分?之前,我试图用一个服务,而不是一个工厂一样,具有相同的错误。希望我没有做一个简单的拼写错误或语法错误,到目前为止,我没有发现任何。

 
  .module('主',[
    ngRoute
  ])  .constant('CAPP',{
      defaultPath':'/家
  })  .factory('svcState',函数(){
      VAR APPSTATE;      功能的getState(){
          返回APPSTATE;
      }      功能的init(){
          APPSTATE ='/家;
      }      在里面();      返回{
          的getState:的getState
      };
  })  的.config(函数($ routeProvider,svcState,CAPP){      $ routeProvider
          。当('/家',{
              templateUrl:谐音/家
          })
          。当('/信息',{
              templateUrl:谐音/信息
          })
          。除此以外({
              // redirectTo:// cApp.defaultPath这工作正常
              redirectTo:svcState.getState //失败错误:[$喷油器:unpr]未知提供商:svcState
          })      });


解决方案

svcState 应该是提供者,而不是服务。由于服务/工厂将不会配置阶段内访问。您需要 svcState 的实施改变供应商,这样它会在角配置块可用。

提供者实现后,你可以使用注入 svcStateProvider 在配置块的供应商。

 的.config(函数($ routeProvider,svcStateProvider,CAPP){  $ routeProvider
      。当('/家',{
          templateUrl:谐音/家
      })
      。当('/信息',{
          templateUrl:谐音/信息
      })
      。除此以外({
          redirectTo:svcStateProvider.getState()//< - 改变herer
      })  })

这个答案 更多地了解供应商

While playing around with Angular I try to understand better how to use factory, services, constants, routing and other core concepts. So I build a simple demo app with node, express, jade and angular.

Now I would like to use a value inside the routeProvider configuration. I created a constant, this works fine. To make it more flexible for future usage I build a factory then, but this fails with "unknown provider". Is this a question of instantiation sequence in Angular? Why I cannot inject the factory into the .config section? Before I tried the same with a service instead of a factory, with the same error. Hopefully I didn't make a simple typo or syntax error, so far I didn't find any.

angular
  .module('main', [
    'ngRoute'
  ])

  .constant('cApp', {
      'defaultPath': '/home'
  })

  .factory('svcState', function(){
      var appState;

      function getState(){
          return appState;
      }

      function init() {
          appState='/home';
      }

      init();

      return{
          getState: getState
      };
  })

  .config(function($routeProvider, svcState, cApp){

      $routeProvider
          .when('/home', {
              templateUrl: "partials/home"
          })
          .when('/info', {
              templateUrl: "partials/info"
          })
          .otherwise({
              //redirectTo: cApp.defaultPath  // this works fine
              redirectTo: svcState.getState   // this fails with "Error: [$injector:unpr] Unknown provider: svcState"
          })

      })

;

解决方案

svcState should be provider rather than service. Because service/factory won't be accessible inside config phase. You need to change the implementation of svcState to provider so that it will be available in config block of angular.

After provider implementation you could inject that provider using svcStateProvider in config block.

.config(function($routeProvider, svcStateProvider, cApp){

  $routeProvider
      .when('/home', {
          templateUrl: "partials/home"
      })
      .when('/info', {
          templateUrl: "partials/info"
      })
      .otherwise({
          redirectTo: svcStateProvider.getState() //<--change herer
      })

  }) 

Look at this answer to know more about provider

这篇关于角&QUOT;未知提供商QUOT; - 如何使用routeProvider配置中的工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆