Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么可以';我是否在指令中访问$location.path()?_Javascript_Angularjs - Fatal编程技术网

Javascript 为什么可以';我是否在指令中访问$location.path()?

Javascript 为什么可以';我是否在指令中访问$location.path()?,javascript,angularjs,Javascript,Angularjs,在我的angular应用程序中,我有一个带有一组链接的菜单,我将链接放在angular指令中,我想使用当前位置来确定链接是否处于活动状态,然后向其添加css类 到目前为止,我所做的是: (剥离至相关材料) angular.module('myNavigation')。指令('navItem',['$location',函数($location){ log($location.absUrl()); log($location.path()); 返回{ 限制:“EA”, 范围:正确, 链接:功能(范

在我的angular应用程序中,我有一个带有一组链接的菜单,我将链接放在angular指令中,我想使用当前位置来确定链接是否处于活动状态,然后向其添加css类

到目前为止,我所做的是: (剥离至相关材料)

angular.module('myNavigation')。指令('navItem',['$location',函数($location){
log($location.absUrl());
log($location.path());
返回{
限制:“EA”,
范围:正确,
链接:功能(范围){
isCurrentLink=angular.equals(scope.item.href$location.absUrl());
},
模板:'”
}
}]);
这很好,但只有当我在导航数据中放入完整的url,包括域名等,但我的指令接收到的导航数据中的url不包含域名/协议等

然后,第一个console.log为我提供完整的URL,但第二个返回空字符串。 由于某些原因,我无法访问指令中的
$location.path()
。我做错了什么

顺便说一句,我有一个路径,我的应用程序不在根文件夹的默认页面上

更新

我刚刚发现$location.path()返回哈希(#)之后的url部分,我希望它返回不带协议、服务器名和服务器端口的url部分

所以当我去的时候,我希望有帖子/25894149/


我可以使用$location来实现此目的吗?

我发现
$location
无法提供此信息

当您包含
$window
时,您可以请求
$window.location.pathname
,这正好满足了我的需要

例如,当您的url为: url:
http://stackoverflow.com/questions/25894149/#something


$window.location.pathname
wil return/questions/25894149/

您看到这个问题了吗?我现在知道了,但我看不出有什么解决办法
angular.module(‘myNavigation’).directive(‘navItem’, ['$location', function($location) {

    console.log($location.absUrl());
    console.log($location.path());

    return {
        restrict: ‘EA’,
        scope: true,
        link: function (scope) {
            isCurrentLink = angular.equals(scope.item.href,$location.absUrl());
        },
        template: ‘<a ng-href=”{{item.href}}” ng-class=”{active:isCurrentLink}”>{{item.name}}</a>”
}
}]);