【问题标题】:Angular JS: result of first resolve won't get passed into the second resolveAngular JS:第一次解析的结果不会传递给第二次解析
【发布时间】:2015-08-02 17:55:12
【问题描述】:

我想弄清楚为什么以下代码会产生 Unknown Provider 错误“geonameProvider

var cacRouteViewMod = angular.module('cacRouteViewMod', ['ngRoute', 'cacLib']);

cacRouteViewMod.config(['$routeProvider', function($routeProvider, $routeParams) {
  $routeProvider    
    .when('/countries/:country/capital', {
      templateUrl: 'countries/country.html',
      controller: 'countryDetailCtrl',
      resolve: {
        geoname: ['$route', 'getGeoname', function($route, getGeoname) {
          return getGeoname($route.current.params.country);
        }],
        country: ['getCountry', 'geoname', function(getCountry, geoname) {
          return getCountry(geoname.countryCode);
        }],
        neighbors: ['$route', 'getNeighbors', function($route, getNeighbors) {
          return getNeighbors($route.current.params.country);
        }]
      }
   })
});

我看到了一些代码,其中给出了几乎相同的代码作为工作示例。就像其他 stackoverflow 帖子 (Angular JS resolve in a resolve) 一样。 还有这篇文章(https://medium.com/opinionated-angularjs/advanced-routing-and-resolves-a2fcbf874a1c)。

为什么我的不工作?

【问题讨论】:

    标签: angularjs routing resolve


    【解决方案1】:

    我相信这是因为您使用的是 ngRoute(AngularJS 中的本机路由模块)而不是 ui-routeur(您的文章中提到的模块)

    尝试使用 ui-router,它应该看起来像:

    .state('countries', {
          url: "/countries/:country/capital", 
          templateUrl: 'countries/country.html',
          controller: 'countryDetailCtrl',
          resolve: {
            geoname: ['$route', 'getGeoname', function($route, getGeoname) {
              return getGeoname($route.current.params.country);
            }],
            country: ['getCountry', 'geoname', function(getCountry, geoname) {
              return getCountry(geoname.countryCode);
            }],
            neighbors: ['$route', 'getNeighbors', function($route, getNeighbors) {
              return getNeighbors($route.current.params.country);
            }]
    
       })
    });
    

    并将其作为依赖注入:

    var cacRouteViewMod = angular.module('cacRouteViewMod', ['ui-router', 'cacLib']);
    

    【讨论】:

    • 谢谢。我在 html 中包含 angular-ui-router.js,切换到 ui.router 依赖,注入 $stateProvider 和 $urlRouterProvider,并使用 $stateProvider.state() 语法。该错误不再显示,但似乎我的 url 没有触发“国家”状态或除 home 之外的任何其他状态 - 它仅包含 url、模板和控制器属性,没有解析。
    • 谢谢。我还发现由于更改,我需要使用 $stateParams 而不是 $route。我编辑了您的帖子并将接受您的回答。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多