如何获取URL AngularJS的最后一部分 [英] How to get the last part of url AngularJS
问题描述
如何在AngularJS中构建函数来chceck url的最后部分?
How to build the function in AngularJS to chceck the last part of the url?
我的例子:
#/ menu1 / 123 / edit
#/ menu2 / 444 / create / new
#/ menu3 / sumbenu1 / 333 / create / new
#/ menu4 / submenu2 / subsubmenu1 / subsubsubmenu1 / 543 / edit
事情是:
123
444
333
543
是在Angular之外生成的ID
123
444
333
543
are the ID's which are generated outside Angular
我需要检查我的网址的最后部分: / edit
或 / create / new
。
I need to check the last parts of my url: /edit
or /create/new
.
网址的长度将是不同(内部网址中 /
的数量)。
The length of the url will be different (number of /
inside url).
我在Angular中有一个控制器来获取网址:
I have a controller in Angular to get the url:
.controller('urlCtrl', ['$scope', '$rootScope', '$location', function ($scope, $rootScope, $location) {
$rootScope.location = $location;
$scope.hashPath = "#" + $rootScope.location.path().toString();
}]);
我正在添加#
字符给该网址因为后来我将它与我的JSON进行比较。
I'm adding the #
character to the url because later I'm comparing it with my JSON.
我试过这个解决方案但它不起作用(我还检查了该主题的其他解决方案,没有发生任何事情)。
I tried this solution but it didn't work (I also checked another solutions from this topic and nothing happened).
有任何想法吗?
编辑:
或者如何只检查编辑
或创建
里面网址?
Or how to check just the edit
or create
inside the url?
我的解决方案是:
var path = $location.path();
var multisearch = path.search(/create|edit|new/);
console.log("multisearch: " + multisearch);
推荐答案
这应该会给你想要的结果:
This should give you the desired result:
window.alert(window.location.href.substr(window.location.href.lastIndexOf('/') + 1));
您可以使用以下方法获取当前网址:window.location.href
然后您只有需要在最后一个'/'
You could get the current URL by using: window.location.href Then you only need to get the part after the last '/'
这篇关于如何获取URL AngularJS的最后一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!