2017-06-15 57 views
0

有什麼辦法如何訪問$route裏面的指令函數?

我基本上是想知道當前templateUrl,說:

app.directive("scrollable", function ($document) { 
    return { 
     restrict: 'A', 
     link: function (scope, element, attrs) { 
      $document.bind('scroll', function (evt) { 
       console.log($route.current.templateUrl); // $route is undefined! 
      }); 
     } 
    }; 
}); 

附:我在AngularJS新手,別那麼嚴謹:)

+0

你有沒有試過把它傳遞給指令? –

+0

你使用什麼路由器?默認或ui路由器? –

+1

你有沒有嘗試注入它像'app.directive(「scrollable」,['$ document','$ route',function($ document,$ route){...}]);' –

回答

5

我認爲你需要的$route注入該指令

app.directive("scrollable", function ($document,$route) { 
+0

謝謝!有用。我會在10分鐘內接受你的答案:) – Andremoniy

+0

@Andremoniy沒問題歡呼聲:D –

2

雖然上面的回答解決您的問題,它總是建議使用像這樣,

app.directive("scrollable", ['$document', '$route', function ($document, $route) { 
}]); 
+1

這將是很好的有一個鏈接*其中*建議,可能*爲什麼*。謝謝。 – Andremoniy

+2

https://stackoverflow.com/questions/13362921/globally-defined-angularjs-controllers-and-encapsulation/13363482#13363482。當你進行生產構建時,它將支持縮小 –