How to Get the Url Parameters Using Angularjs

Reading URL parameters in AngularJS - A simple way?

You can inject the $location service if you are using html5 mode.

Because you are using URLs without a base root (such as a ! or a #), you need to explicitly add a <base> tag that defines the "root" of your application OR you can configure $locationProvider to not require a base tag.

HTML:

<head>
<base href="/">
...
</head>
<body>
<div ng-controller="ParamsCtrl as vm">
...
</div>
</body>

JS:

app.config(['$locationProvider', function($locationProvider){
$locationProvider.html5Mode(true);

// or

$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
}]);

app.controller("TestCtrl", ['$location', function($location) {
var params = $location.search();
alert('Foo is: ' + params.foo + ' and bar is: ' + params.bar);
});

It is worth noting that you can add a router library such as ngRoute, ui-router or the new, unfinished component router. These routers rely on a base route off of a symbol (! or #) and define everything after the symbol as a client-side route which gets controlled by the router. This will allow your Angular app to function as a Single Page App and will give you access to $routeParams or the ui-router equivalent.

Get url parameters query string in AngularJS

You can check

https://docs.angularjs.org/api/ngRoute/service/$routeParams

also routing can be hell sometimes so please look at this solution.. that saved my life

https://ui-router.github.io/guide/states

Access URL parameters in Angularjs in the Controller

After spending some time, i figured that $location is the one I was searching for. Below is sample code snippet. >>AngularJS v1.4.8

main.js

     var mainApp = angular.module("app",['ui.bootstrap','ngModal']);
mainApp.config(['$locationProvider',function($locationProvider){
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});

var mainController = function($scope,$window,$rootScope, $cookies,$cookieStore,$location){
var searchObject = $location.search();

window.alert("parameter is :.."+searchObject.param1);
window.alert("Url typed in the browser is: "+$location.absUrl());
}

mainApp.controller("MainController",["$scope","$window","$rootScope",'$location',mainController]);

Enter the this URL in the browser:
http://localhost:8180/Application/#param1=test

Output:

alert 1: parameter is :.. test

alert 2: "Url typed in the browser is: " +http://localhost:8180/Application/#param1=test

How to get the value from URL parameter in AngularJS?

Avoid using ng-init instead use controller

function SomeController($scope, $location){
var fname = $location.search().fname;

}

Angularjs - How can I get parameters from URL

Ugly unsecure two-liner:

paramsObject = {};
window.location.search.replace(/\?/,'').split('&').map(function(o){ paramsObject[o.split('=')[0]]= o.split('=')[1]});

paramsObject will be:

Object {location: "123", arrival: "01", departure: "456", rooms: "789"} 

which you can inject directly into your $scope.parameters

plunker here

Get URL parameters using AngularJs

You can just add ?opt to the url

If you need to have more than one, separate them with an '&'

.config(function($stateProvider) {
$stateProvider
.state("testing", {
url: "/testSession/:id?otp",
controller: "UserTestSessionController",
templateUrl: "./app/components/userTestSession/user-test-session.html",
controllerAs: "vm"
})
})


Related Topics



Leave a reply



Submit