1

If I wanted to get a specific value from the URL within Angular I'm looking at how to parse this data.

Example:

 http://localhost:1337/doc-home/#/tips/5?paginatePage=1

I want to get the "5"

HTML:

<a href="#/tips/comments/{{ tip.id }}?groupid={{ ????? }}" class="btn-yellow">Add / View Comments</a>

module.js

 .when('/tips/comments/:group?id', {
    templateUrl: 'partials/comments.html'

controller.js

  .controller('TipCommentsCtrl',

     function ($http, $scope, $routeParams, Categories, Tip, $location, error, $sce) {

UPDATE

I am getting CLOSER thanks to the answers from you all.

I posted another question which should help to explain what my problem is

Angular routeParams not working URL is changing

Community
  • 1
  • 1

3 Answers3

1

The $routeParams service gives you the ability to access the route parameters and returns an object.

In order to get the parameter group in your example /tips/comments/group?id.. use $routeParams.group

onel0p3z
  • 566
  • 5
  • 11
1

Based on your requirements you can choose any of either:

  1. If you're using ngRoute, you can inject $routeParams into your controller.ngRoute is a angular core module which is good for basic scenarios. URL: https://docs.angularjs.org/api/ngRoute http://docs.angularjs.org/api/ngRoute/service/$routeParams

    If you are using the RouteProvider and routeParams: The route wires up the URL to your Controller/View and the routeParams can be passed into the controller. Check out the Angular seed project. Within the app.js you'll find an example for the route provider. To use params simply append them like this:

       $routeProvider.when('/view1/:param1/:param2', {
         templateUrl: 'partials/partial1.html',    
         controller: 'MyCtrl1'
       });
    

    Then in your controller inject $routeParams:

       .controller('MyCtrl1', ['$scope','$routeParams', function($scope,     
       $routeParams) {
         var param1 = $routeParams.param1;
         var param1 = $routeParams.param2;
         ...
       }]);
    

Also can get other arbitrary params in the query string form /view/1/2?other=12 with $routeParams.other.

  1. Ui-router is a contributed module which is overcome the problems of ngRoute. Mainly Nested/Complex views.If you're using angular-ui-router, you can inject $stateParams. URL: https://github.com/angular-ui/ui-router, https://github.com/angular-ui/ui-router/wiki/URL-Routing.

    For Example : http://angular-ui.github.io/ui-router/sample/#/

Though your answer is specifically on how to get the parameters here i have added more relevant information on what to choose and how to use. I hope it helps. Thank you.

road2victory
  • 486
  • 4
  • 12
0

You will have the details in $routeParams

$routeParams.group

Vishnu
  • 11,614
  • 6
  • 51
  • 90