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

标签 angularjs ngroute resolve

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

根据文档:

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.

...但是,在我的自定义指令的链接函数中,$resolvescope 属性上未定义 - 即使我观察它的更改也是如此。

为什么$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 中的新内容。

关于angularjs - Angular ngRoute : Why is $resolve not available on scope in directive link function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34957404/

相关文章:

javascript - 在服务中使用服务 Angularjs

javascript - 如何创建嵌套在 html 内容中的基础弹出窗口(模板)

docker nginx ERR_NAME_NOT_RESOLVED

android - 无法解析主机 Android

C# 无法解析符号

javascript - Angular ngModel 不会触发 Kendo 更改事件

javascript - 在执行前一个事件的处理程序之前检查下一个事件以隐藏自动完成

javascript - 为什么 "this"被分配给 Controller 内部的变量?

angularjs - Angular ngRoute - 如果手动输入网址,则无法获取页面

angularjs - 从 AngularJS 路由中删除 #