是否有可能获得在角JS一个指令当前的$ location?

问题描述 投票:12回答:2

沿着线的东西:link: function($scope, element, attrs, $location) {

这可能吗?

angularjs angularjs-directive
2个回答
23
投票

在指令的声明,注入的位置服务。

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服务。

两个信息:

  1. $route Docs
  2. $location Docs

3
投票

以为注射服务为条指令,它仍然得到未定义为链接功能,对于需要使用

location.hash = "#/path_name";

在链接功能,将工作

© www.soinside.com 2019 - 2024. All rights reserved.