0

I have MVC application, I want make routing for the following URL

http://localhost:2668/Home/User/1

I try

.config(function ($routeProvider, $locationProvider) {
//here we will write code for implement routing 
$routeProvider
.when('#/Home/User/:userid', {
    templateUrl: '/AngularTemplates/User.html',
    controller: 'UserController'
})

.otherwise({   // This is when any route not matched
    controller: 'ErrorController'
}) })

And then the UserController as below:

.controller('UserController', ['$scope','$routeParams', function ($scope,$routeParams) {
    // $routeParams used for get query string value

    //var loc = window.location.href;
    //var id = loc.slice(loc.lastIndexOf('/'), loc.length).split('/')[1];
    $scope.Message = "This is ORDER Page with query string id value =" + $routeParams.userid;
}])

But I always get "undefined" for the para $routeParams.userid

What is wrong in my code? Please help thanks!

4
  • 1
    Just remove # from the when condition.You doesnt need it. Commented Oct 29, 2015 at 8:44
  • Thanks but it still "undefined". Commented Oct 29, 2015 at 8:53
  • 1
    :userid} should be :userid Have a look at "}". Commented Oct 29, 2015 at 8:57
  • Thanks I just remove it but it still not works correct. Maybe duplicate with MVC routing? Commented Oct 29, 2015 at 9:01

1 Answer 1

1

Here is a working example:

<!DOCTYPE html>
<html ng-app="app">

  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular-route.js"></script>
  </head>

  <body>
    <h1>Hello Plunker!</h1>

    <a href="/Home/User/11">user 11</a>
    <a href="/Home/User/12">user 12</a>

    <div ng-view></div>

    <script>
      var app = angular.module('app', ['ngRoute']);

      app.config([
          '$locationProvider', '$routeProvider',
          function ($locationProvider, $routeProvider) {

              $locationProvider.html5Mode({
                  enabled: true,
                  requireBase: false
              }).hashPrefix('!');

              $routeProvider
                .when('/Home/User/:userid', {
                    template: '<pre>{{ message }}</pre>',
                    controller: 'UserController'
              });
          }]);

      app.controller('UserController', ['$scope','$routeParams', function ($scope, $routeParams) {
          $scope.message = "userid = " + $routeParams.userid;
      }]);
    </script>
  </body>

</html>

JSBin http://output.jsbin.com/geluli

You may read more about default router here (nice examples + tests) https://docs.angularjs.org/api/ngRoute/service/$route

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

1 Comment

Nice! But .hashPrefix('!') is not really required?!

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.