Reputation: 520
How can I get parameter from the current URL ?
I have URL http://localhost/admin.brands/edit?brandid=1&brandname=samsung&isactive=1. For instance, how can I get brandid from the URL ?
I will modify this:
<script>
var app = angular.module('myApp', [])
.controller("PostsCtrl", function($scope, $http) {
$http.get('http://localhost/admin.brands/getJsonBrandAndEdit?brandid=XX').
success(function(data, status, headers, config) {
$scope.post = data;
}).
error(function(data, status, headers, config) {
});
});
</script>
Upvotes: 0
Views: 639
Reputation: 47804
You should probably be using angular router or ui router (Would recommend the later one) and pass these values which are currently being passed in the query param in the route, would be much easier to read the values then from your controller.
Would have been something like this
.controller('PostsCtrl',
[ '$scope','$stateParams'
function($scope , $stateParams ) {
//
$scope.brandId= $stateParams.brandid;
...
Incase you dont want to do it and need a quick fix, here is something that would work...
JavaScript:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
Usage:
var brandId = getParameterByName('brandid');
Source : How can I get query string values in JavaScript?
Upvotes: 3