2016-01-22 58 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

我猜這有助於閱讀手冊的正確版本。謝謝! –

相關問題