2

I have to pass multiple ids to a service via route. Right now the route works for a single id, but as a beginner to angular JS, I don't know how to pass multiple ids or is there a way where I can pass an array of ids via route? I am stuck and looking for some assistance

current route config

.when('/search/:id')

Expecting

.when('/search/:id/:id/:id')

or .when('/search/ids[]')

Thanks for the help in advance.

1
  • 2
    I would suggest you manually join the ids with "," or ";" and split them in the handler Commented Jun 14, 2013 at 22:37

2 Answers 2

2

Use .when('/search/:id1/:id2/:id3') or separate multiple ids with comma or other char.

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

Comments

0

This is what I came up with. It's by no means perfect or bulletproof but it's a start.

Demo

PS: I only used the same controller for conveniance

app.config(['$routeProvider', function($routeProvider) {
  $routeProvider
   .when('/home:matrix', {templateUrl: 'home.html', controller: 'MainCtrl'})
   .when('/about:matrix', {templateUrl: 'about.html', controller: 'MainCtrl'});
}]);

function extractMatrixParams(matrix) {
  var matrixRegexp = /([^;=]+)=([^;]+)/g;
  var matrixParams = {};
  var match = matrixRegexp.exec(matrix);
  while(match) {
    var p = matrixParams[match[1]];
    if(p) {
      if(angular.isArray(p)) {
        p.push(match[2]);
      }
      else {
        matrixParams[match[1]] = [p, match[2]];
      }
    }
    else {
      matrixParams[match[1]] = match[2];
    }
    match = matrixRegexp.exec(matrix);
  }
  return matrixParams;
}

app.controller('MainCtrl', ['$scope', '$routeParams',
function($scope, $routeParams) {
  var matrixPrams = extractMatrixParams($routeParams.matrix);

  $scope.ids = matrixPrams.ids;
  $scope.myVar = matrixPrams.myVar;
  $scope.matrixPrams = matrixPrams;

  $scope.routeParams = $routeParams;
}]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.