Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using angularjs inside my asp.net mvc project and have the folowing link in the asp.net view:

<a href="/Organization/Employee/@user.Alias#info">@user.Alias.ToUpper()</a>

that gives me a link like this for example:

http://localhost:53315/Organization/Employee/15#/info

Which I want to be hanled by mvc controller and then use angularjs routing to show template inside returned asp.net view.

Here's my asp.net mvc controller:

      public ActionResult Employee(String id)
        {
          ...
          return View();
        }

routeConfig:

    context.MapRoute(
       "Organization_Employee",
       "Organization/Employee/{id}/{*mod}",
       new { controller = "User", action = "Employee" }
   );

and angularjs stuff:

 $routeProvider
      .when('/info', {
        controller: 'UserCtrl',
        templateUrl: baseTemplateUrl + 'info.html'
    })

    .controller('UserCtrl', ['$scope', '$location', '$routeParams', 'User', function ($scope, $location, $routeParams, User) {
        $scope.user = User.get({ id: $routeParams.id });

How I can get that id('15') parameter from url in angular controller?

share|improve this question
    
why are you using mvc routing side by side with angularjs? I would go rather with one option (angularjs or mvc routing). –  MajoB Oct 14 '14 at 18:20

1 Answer 1

up vote 1 down vote accepted

In your case you can get id by parse window url $window.location.href or $location.absUrl()

.controller('UserCtrl', ['$window', '$location', '$routeParams', 'User', function ($window, $location, $routeParams, User) {
    var splitUrl=$window.location.href.split("#")[0].split("/");
    //or 
    //var splitUrl=$location.absUrl().split("#")[0].split("/");
    var  userId=splitUrl[splitUrl.length]; // some like that
    $scope.user = User.get({ id: userId });

If your url looks like http://localhost:53315/Organization/Employee/#/info/15 or http://localhost:53315/Organization/Employee/15#/info/15

. You can get id that way:

 $routeProvider
  .when('/info/:id', {
    controller: 'UserCtrl',
    templateUrl: baseTemplateUrl + 'info.html'
})

.controller('UserCtrl', ['$scope', '$location', '$routeParams', 'User', function ($scope, $location, $routeParams, User) {
    $scope.user = User.get({ id: $routeParams.id });

This is because $location work with path after hashbang in non HTML5 Mode.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.