Angular ngRoute:为什么 $resolve 在指令 link 函数的范围内不可用?

Angular ngRoute: Why is $resolve not available on scope in directive link function?

在我使用 ngRoute 的 Angular v1.4.8 应用程序中,我有一个带有解析的路由,我想在自定义指令的 link 函数中访问它。

根据文档:

For easier access to the resolved dependencies from the template, the resolve map will be available on the scope of the route, under $resolve (by default) or a custom name specified by the resolveAs property.

...然而 $resolve 在我的自定义指令的 link 函数中的 scope 属性 上未定义——即使我观察它的变化也是如此。

为什么 $resolvescope 上不可用?

代码: (JSFiddle)

angular.module('myApp', ['ngRoute'])
  .config(routeConfig)
  .directive('myDirective', myDirective)
;

function routeConfig($routeProvider) {
  $routeProvider
    .when('/', {
      resolve: {
        data: function($q) {
          var deferred = $q.defer();
          deferred.resolve('foo');
          return deferred.promise;
        }
      },
      template: '<my-directive></my-directive>'
    })
    .otherwise('/')
  ;
}

function myDirective($route) {
  return {
    link: function(scope, iElement, iAttrs) {
      console.log($route.current.locals); // contains data
      console.log(scope.$resolve); // undefined
      scope.$watch(function() {
        return scope.$resolve;
      }, function(newValue) {
        console.log(newValue); // undefined, never changes
      });
    }
  };
}

此段在 1.5 版本的文档中,但不在您使用的 1.4.x 版本的文档中。所以这是 1.5 中的新内容。