Is there a way to create a function in AngularJS that can check the last segment of a URL?
Here are some examples:
#/menu1/123/edit
#/menu2/444/create/new
#/menu3/submenu1/333/create/new
#/menu4/submenu2/subsubmenu1/subsubsubmenu1/543/edit
The key points here are:
123
, 444
, 333
, and 543
are IDs generated externally outside of Angular
The goal is to validate the last segments of the URLs: /edit
or /create/new
.
The length of the URL may vary (depending on the number of slashes inside).
In my Angular controller, I have code to retrieve the URL:
.controller('urlCtrl', ['$scope', '$rootScope', '$location', function ($scope, $rootScope, $location) {
$rootScope.location = $location;
$scope.hashPath = "#" + $rootScope.location.path().toString();
}]);
I'm appending the #
symbol to the URL for comparison with JSON later on.
I attempted this proposed solution without success (tried other solutions from the thread as well).
Any insights or suggestions?
EDIT:
Alternatively, how can we specifically check for edit
or create
within the URL?
My approach so far is:
var path = $location.path();
var multisearch = path.search(/create|edit|new/);
console.log("multisearch: " + multisearch);