1

I want to get a querystring which is part of the actual route, this is the actual url:
http://localhost:13453/Vehicle/Profile/c2db202f-9bf0-4876-851c-29964484bf7a

I want to get the last value which comes after Profile/

How can I achieve that in angularJS, which I can then use to get the details like:

.controller('VehicleProfileController', ['$scope', '$http', '$location',
  function ($scope, $http, $location) {
    //get the id here which I'll pass to $http call

     $http({
         method: 'GET',
         url: '/Vehicle/GetProfile'
     }).
     success(function (data) {

     });

}])
1
  • What router are you using? ui-router or ngRoute? Commented Oct 20, 2015 at 15:17

3 Answers 3

1

If you just need a straight javascript solution, you can use something like the following without having to wire up other dependencies:

var loc = window.location.href; 
var id = loc.slice(loc.lastIndexOf('/'), loc.length).split('/')[1];

Here's a quick JSFiddle to play with.

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

1 Comment

I think he want to use angular in this process.
0

You can use $stateProvider:

angular.module('vehicles').config(['$stateProvider',
    function($stateProvider) {
      $stateProvider.
            state('vehiclesId', {
                url: '/Vehicle/Profile/:id',
                templateUrl: 'modules/view.client.view.html'
            });

}]);

than use just in controller as:

angular.module('vehicles').controller('VehiclesController', ['$stateParams',
function($stateParams){

       $stateParams.id

}]);

2 Comments

any where, just before the controller for example
@Laziale please try using my code, I'm sure will do the work!
0

As @sma said in his comment, we would need to know what router are you using.

ui-router

In case you were using ui-router and if you defined your params in your state, you could just use $stateParams in order to get it in your controller:

app.controller('MyController', ['$stateParams', function ($stateParams) {
    var id = $stateParams.myParamName; // the param defined in the state
}]);

ngRoute

In case you were using ngRoute, you should use $routeParams in the same way:

app.controller('MyController', ['$routeParams',
    function($routeParams) {
        var id = $routeParams.myParamName; // the param defined in your route
}]);

EDIT

I'm going to assume you are using ngRoute, so first of all, check your route definition, it should be in your config phase, and it should look like this:

.config(['$routeProvider',
    function($routeProvider) {
    $routeProvider.
      when('/phones', {
        templateUrl: 'partials/phone-list.html',
        controller: 'PhoneListCtrl'
      }).
      when('/phones/:phoneId', {
        templateUrl: 'partials/phone-detail.html',
        controller: 'PhoneDetailCtrl'
      }).
      otherwise({
        redirectTo: '/phones'
      });
}]);

And then, in your controller, you should be able to get your param (called phoneId in this example) as in the above ngRoute controller example (with $routeProvider).

Probably you should first take a look to the documentation, particulary to this part.

3 Comments

I'm not sure, where can I check the router? Is default I got when I installed from nuget. Angular is used within MVC app
Then I'll assume that you are using ngRoute, the default one. How did you define your routes/states?
I haven't defined anything specific for router/states, they are default within mvc. Do I need to check them somewhere? This is what I have in mvc code i.gyazo.com/96afd1ec61127d8c748b43acc6145951.png

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.