user3610227
user3610227

Reputation:

$routeParams returning an empty object

an angular application.

var self = this;
angular.module("myForm", ["ngRoute"])
     .controller("Controller", ["$scope", "$routeParams", function($scope, $routeParams){self.controller($scope, $routeParams)}]);

angular.bootstrap(document.getElementById("form"), ["myForm"]);

It's returning an empty object, {}

while the url is

http://localhost:3000/admin/event/form/?id=1

Controller:

controller: function($scope, $routeParams) {
    console.log($routeParams);
},

Edit

I read to add ng-view. I did but same problem

<span ng-view></span>

The route is not really changing, I want to read this value on page load only.

Upvotes: 2

Views: 537

Answers (1)

manasi sakhare
manasi sakhare

Reputation: 1051

The pattern ?id=1 in your url is not the correct way of passing route params. And to support route params, one should define the routes and the params which it supports. Route is defined in config block as (referred example from angular docs):

app.config(function($routeProvider, $locationProvider) {
  $routeProvider
   .when('/Book/:bookId', {
    templateUrl: 'book.html',
    controller: 'BookController',
    resolve: {
      // I will cause a 1 second delay
      delay: function($q, $timeout) {
        var delay = $q.defer();
        $timeout(delay.resolve, 1000);
        return delay.promise;
      }
    }
  });
});

To access the route and pass param to the above defined route, one must construct the url as:

<protocol><domain prefix>/Book/123

So /Book is your route and 123 is the param value of bookId. in your $routeParams it is available as $routeParams.bookId

Upvotes: 1

Related Questions