2

How to access the URL parameters through the angularJS controller. For example: i would like to read parameter1 and parameter2 in a controller so that i can take appropriate action in my controller. http://localhost:8080/MyApplication?Parameter1=testresponse&parameter2=1234

3 Answers 3

9

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

Sign up to request clarification or add additional context in comments.

1 Comment

Appreciated it !! Self Explored Great !! Barani !!
1

Inject $routeParams in your controller like

angular.module(yourapp).controller(yourcontroller,['$routeParams',
function($routeParams){
    console.log($routeParams.Parameter1) //Prints test response
    }]);

Comments

0

Using $routeParams service you can access url params. In your case

angular.module('myApp', [])
 .controller('myController', (function ($scope, $routeParams) {
   $routeParams.Parameter1 // for 1st param
   $routeParams.parameter2 // for second param
});    

For more information - $routeParams

3 Comments

Getting "Param...undefined" error for angular.module('myApp', []) .controller('myController', (function ($scope, $routeParams) { window.alert("Param.."+$routeParams.Parameter1); // for 1st param $routeParams.parameter2 // for second param });
please create a fiddle so we can reproduce your problem and fix it
i was looking for $location. Anyways...Thanks for your time.:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.