2016-01-22 68 views
2

在我使用ngRoute的Angular v1.4.8应用程序中,我有一个想要在自定义指令的链接函数中访问的解决方案。

根据文档:

对于从模板解析的依赖更容易获得,在 决心地图将可在路由的scope,下 $resolve(默认)或自定义名称由resolveAs 属性指定。

...但$resolve未定义在我的自定义指令的链接功能的scope财产 - 甚至当我看着它更改。

为什么$resolve不适用于scope

代码: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 
     }); 
    } 
    }; 
} 

回答

3

本款是1.5版本的文档中,但不是的1.4.x的版本的文档中,您使用。所以这是1.5中的新东西。

+0

相关的拉取请求(标记为1.5.0-rc.0):https://github.com/angular/angular.js/pull/13400 –

+0

我猜这有助于阅读手册的正确版本。谢谢! –

相关问题