1

Using AngularJS (v1.3.8) I try to read a GET parameter from a URL which looks like this: http://www.foo.com/whatever1?keyword=bla

Using the following code, keyword for whatever reason remains undefined:

HTML:

<!doctype html>
<html ng-app="myApp">
<head>
    <title>test</title>
    <script src="/js/angular.min.js"></script>
    <script src="/js/angular-route.min.js"></script>
    <script src="/js/myangularscript.js"></script>

</head>
<body ng-controller="myController">

[...]

</body>
</html>

Javascript (myangularscript.js):

    myApp = angular.module('myApp',['ngRoute']);

    myApp.controller('myController', function ($scope,$http,$routeParams,$location) {
        alert($routeParams.keyword); //undefined
        alert($location.search().keyword); //undefined
    });

Added route definition (has to be placed where exactly??):

myApp.config(['$routeProvider',
        function($routeProvider) {
            $routeProvider.
            when('/whatever1/:keyword', {
                controller: 'myController'
            });
        }]);
Igor P.
  • 1,407
  • 2
  • 20
  • 34
  • and your route definition is... ? There's always `$location.search().keyword` – Phil Jul 07 '15 at 23:53
  • Are you defining the route somewhere? – Mindastic Jul 07 '15 at 23:53
  • I added a route definition, unsure whether it's correct or where to place it exactly. – Igor P. Jul 07 '15 at 23:59
  • `$routeParams` only contains parameters that are defined in your route. AFAIK, `ngRoute` only supports path parameters. You will have to use `$location` as indicated in my first comment or move to `ui-router` which does support query parameters – Phil Jul 07 '15 at 23:59
  • Like Phil said, if you want to use your route setup, change the url to http://www.foo.com/whatever1/bla and then your routeparams should give you the right keyword value. – Krishna Veeramachaneni Jul 08 '15 at 00:16
  • I'd like to stick to the current format, thus I can't use ngRoute as Phil pointed out. I changed the code and added $location but still can't read keyword. – Igor P. Jul 08 '15 at 00:21
  • It is somewhat painless to switch to ui-router, which will pass those parameters into $stateParams and ui-router gives you much more flexibility as well as features. https://angular-ui.github.io/ui-router/site/#/api/ui.router – Sina Khelil Jul 08 '15 at 00:26
  • Seems I'm incorrect about `$routeParams` and `$location.search`. According to the [docs](https://docs.angularjs.org/api/ngRoute/service/$routeParams), `$routeParams` should include query parameters. [This Plunker](http://plnkr.co/edit/7KVlbv13VIrq32eapSgs?p=preview) works as expected too – Phil Jul 08 '15 at 01:08
  • Based on your template definition of the controller in the body tag, you're not actually using angular routes and I suspect your intention is not to use routes. So $routeParams will not work. You're better off using something like [this](http://stackoverflow.com/questions/19491336/get-url-parameter-jquery). If you want to use $routeParams then you will have to change your program to use angular routes. Your url would look like http://www.foo.com/#whatever1?keyword=bla – logee Jul 08 '15 at 01:13
  • As I said, I'd like to stick to the plain URL format (foo.com/whatever1?keyword=bla), not using hashtag notation. Thus, as user2341963 proposed, I decided to grab the entire URL using document.location and manually extracting the parameters employing string operations. – Igor P. Jul 09 '15 at 12:22

0 Answers0