-1

I see this documentation: http://docs.angularjs.org/api/ng.directive:ngHref

And this clearly shows that you can manipulate the URL based on values in an input.

But I want to do the opposite. I want to have http://www.example.com/5 and feed the 5 to a variable I can use in my controller.

How do I do that?

itamar
  • 3,837
  • 5
  • 35
  • 60
  • You can use [__$routeParams__](http://docs.angularjs.org/api/ngRoute.$routeParams) – Satpal Feb 14 '14 at 19:49
  • @Satpal Thanks - but I htink the documentation is somewhat incomplete. Do I really just plug `$routeParams ==> {chapterId:1, sectionId:2, search:'moby'}` into my controller? That seems to not be the full implementation. – itamar Feb 14 '14 at 19:51
  • a good example http://stackoverflow.com/questions/11534710/angularjs-how-to-use-routeparams-in-generating-the-templateurl – Satpal Feb 14 '14 at 19:57

2 Answers2

2

Use $routeParams service to get the ID.

see as an example

angular.module('myApp', []).config(function ($routeProvider, $routeParams) {
     $routeProvider.when('/:id', {templateUrl: 'example.html', controller: 'exampleCtrl'});
});

And to get the id as a variable user $routeParams in your controller.

myApp.controller('exampleCtrl', function($scope, $routeParams) {
    alert($routeParams.id);
});
Hassan Siddique
  • 1,590
  • 14
  • 27
1

I would call a service from my controller, and then assign scope based on the output of that function.

If you already have the values for {chapterId:1, sectionId:2, search:'moby'}, you can either assign them to a shared scope, or cache them in your service, and retrieve them when you call your other controller

koolunix
  • 1,995
  • 16
  • 25