AngularJS - 删除查询字符串并比较路径

时间:2017-08-09 14:05:06

标签: angularjs query-string

$scope.currentLocationPath = $location.path();

$scope.configUserPath = '/configUser';

    http://myurl.com/#/configUser/

$scope.editUserPath = '/editUser';

    http://myurl.com/#/editUser/1

<div ng-hide="(currentLocationPath === configUserPath || currentLocationPath === editUserPath)>do something</div>

我正在使用当前位置比较路径。对于没有查询字符串的网址,它运行良好,但不会将网址与查询字符串的正确路径进行比较,如编辑用户案例中所示。

如何在editUser之后删除查询字符串并将其与currentlocation进行比较?

1 个答案:

答案 0 :(得分:0)

如果您有以下网址:

http://myurl.com/#/editUser/1

然后$location.path()将返回给您:/editUser/1,以便您可以将其拆分为字符/,获取第二个元素并在开头添加/(因为{ {1}}删除每个split),如下所示:

/

它会回复你:

'/' + $location.path().split('/')[1]

这也适用于您拥有以下网址的情况(它会返回给您:/editUser ):

/configUser