2

I am trying to get URL parameters using $routeParams:

var myApp = angular.module('myApp', [ 'ngRoute', 'appControllers', 'appFilters', 'appServices' ]).config(['$routeProvider', function($routeProvider) { $routeProvider.when('/admin/organisations/:organisation_id', { controller: 'UpdateOrganisationCtrl' }); }]); 

and in my Controller:

appControllers.controller('UpdateOrganisationCtrl', ['$rootScope', '$scope', '$http', '$window', '$routeParams', function($rootScope, $scope, $http, $window, $routeParams) { console.log($routeParams.organisation_id); }]); 

However I am printing undefined as $routeParams is {} Any thoughts?

9
  • What's the URL that you are using to access this? It would seem there are no parameters in the URL? Commented Mar 1, 2016 at 16:00
  • I believe you have to specify params in the route. Commented Mar 1, 2016 at 16:01
  • The only thing I can think is maybe the underscore in organisation_id is breaking things? Does renaming it as organisationid help atall? Commented Mar 1, 2016 at 16:01
  • @Rhumborl That was what I thought, but renaming to organisationid did not help at all. Thanks anyways. Commented Mar 1, 2016 at 16:03
  • @cale_b For example: localhost:3000/admin/organisations/56cde4bf911747ea200d5a63 Commented Mar 1, 2016 at 16:03

1 Answer 1

2

The url you tried to access should be

localhost:3000/#/admin/organisations/56cde4bf911747ea200d5a63

ngRoute will make your application a Single Page Application, where the relevant part of the url for the router will be after # (beautifully called the hashbang).

Look at the example:

// Given:
// URL: http://server.com/index.html#/Chapter/1/Section/2?search=moby
// Route: /Chapter/:chapterId/Section/:sectionId
//
// Then
$routeParams ==> {chapterId:'1', sectionId:'2', search:'moby'}

However, the url you give could work if you use HTML5Mode for $location service of angular.

See those links for more information:

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

3 Comments

Thanks! I tried to do $locationProvider.html5Mode(true) but $routeParams were still {} Any thoughts?
have you verifiy that that url works : localhost:3000/#/admin/organisations/56cde4bf911747ea200d5a63 ?
Unfortunately, I cannot because my application is not a SPA and the URL with a hashbang is redirected to / which is redirected to /dashboard on the server side. So overall I get a redirect to localhost:3000/dashboard#/admin/organisations/56cde4bf911747ea200d5a63

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.