Angularjs 是否可以在Angular JS中的指令中获取当前$location?

Angularjs 是否可以在Angular JS中的指令中获取当前$location?,angularjs,angularjs-directive,Angularjs,Angularjs Directive,类似于:link:function($scope,element,attrs,$location){ 这可能吗?在指令声明中,插入位置服务 app.directive('myDirective', ['$location', function($location) { return { link: function(scope, elem, attrs) { // path() and url() can be used as getters or setters conso

类似于:
link:function($scope,element,attrs,$location){


这可能吗?

在指令声明中,插入位置服务

app.directive('myDirective', ['$location', function($location) {

 return {
  link: function(scope, elem, attrs) {
   // path() and url() can be used as getters or setters
   console.log($location.url());
   console.log($location.path());
  }
 };
}]);
如果您试图获取当前位置,请使用
location.path()
或使用
$route
服务

这两方面的信息:


  • 虽然将服务注入到指令中,但它仍然未定义到链接函数中, 为此,您需要使用

    location.hash = "#/path_name";
    

    在链接功能中,它将起作用

    但是,当我切换到另一个页面时,位置不会更新。您是否正在尝试使用位置服务获取他们所处的位置?是的。这就是目标。我已更新了我的答案以提供一些附加提示。您需要使用位置服务或路径服务,但这取决于您的选择你想要路由。我想要路径,特别是
    $location.path()
    ,但在切换页面时它不会更新。