Angular JS:第一次解析的结果不会传递到第二次解析中

Angular JS: result of first resolve won't get passed into the second resolve

我正在努力弄清楚为什么以下代码会产生未知提供程序错误 "geonameProvider <- geoname <- country"

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

我见过一些代码,其中给出了几乎相同的代码作为工作示例。像其他 Whosebug post ()。 还有这篇文章(https://medium.com/opinionated-angularjs/advanced-routing-and-resolves-a2fcbf874a1c).

为什么我的不行?

我认为这是因为您使用的是 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']);