0

I'd like to get the value of specific url parameters using AngularJS. I then want to assign a specific value to a specific textbox.

An example URL might look like:

http://example.com/?param1=value1

I've seen examples about $location, routing and services. I don't want to do any of that. I just need the value of param1. Any ideas how that can be done?

Here's a corresponding jsfiddle.net with several attempts: http://jsfiddle.net/PT5BG/211/

4thSpace
  • 43,672
  • 97
  • 296
  • 475

3 Answers3

2

If you are using ngRoute, look for routeParams

If you using ui-Router, look for stateParams

JS way:

var key = 'param1';
var value = window.location.search.substring(window.location.search.indexOf(key)+key.length+1);
Aniket Sinha
  • 6,001
  • 6
  • 37
  • 50
2

Using $location is the angular way

$location.search().param1; should give it if html5mode=true

Otherwise you have to use pure javascript. Check this. How can I get a specific parameter from location.search?

Community
  • 1
  • 1
Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42
  • For html5mode, where does this line go? $locationProvider.html5Mode(true).hashPrefix('!') I'm not really sure what a hashbang is used for. I'll be separating url key/values by question mark. – 4thSpace Dec 13 '14 at 06:47
0

Try this.You need to route concept in angular js

  <!DOCTYPE html>
    <html lang="en">
    <head>
        <title>AngularJS Routes example</title>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular-route.min.js"></script>
    </head>

    <body ng-app="sampleApp">

    <a href="#/route1/abcd">Route 1 + param</a><br/>
    <a href="#/route2/1234">Route 2 + param</a><br/>

    {{param}}
    <div ng-view></div>

    <script>
        var module = angular.module("sampleApp", ['ngRoute']);

        module.config(['$routeProvider',
            function($routeProvider) {
                $routeProvider.
                        when('/route1/:param', {
                            templateUrl: 'your_url1',
                            controller: 'RouteController'
                        }).
                        when('/route2/:param', {
                            templateUrl: 'your_url2',
                            controller: 'RouteController'
                        }).
                        otherwise({
                            redirectTo: '/'
                        });
            }]);

        module.controller("RouteController", function($scope, $routeParams) {
            $scope.param = $routeParams.param;
            alert($scope.param);
        })
    </script>        

    </body>
    </html>  

$routeParams allows you dto the value of parameter

vaibhav kulkarni
  • 1,733
  • 14
  • 20