是否可以在 Angular JS 的指令中获取当前的 $location? [英] Is it possible to get the current $location in a directive in Angular JS?

查看:38
本文介绍了是否可以在 Angular JS 的指令中获取当前的 $location?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这可能吗?

推荐答案

在指令的声明中,注入定位服务.

In the declaration of the directive, inject the location service.

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

If you are attempting to get the current location, use location.path() or, alternatively, use the $route service.

关于两者的信息:

  1. $route 文档
  2. $location 文档

这篇关于是否可以在 Angular JS 的指令中获取当前的 $location?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆