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/

3 Answers 3

3

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?

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

1 Comment

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.
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);

1 Comment

No routing as mentioned in the OP.
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

1 Comment

Since wordpress processes first, this won't work. It needs to be query parameters.

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.