0

** --Routing is not working on Angular JS . Please help. HTML code and js file attached. --** Below is my HTML code

<!DOCTYPE html>
<html>
<head>
    <title>AngularJS Routing Examples</title>
    <meta charset="utf-8" />
    <link href="Content/bootstrap.css" rel="stylesheet" />
</head>
<body ng-app="sampleApp">
    <div class="container">
        <div class="row">
            <div class="col-md-3">
                <ul class="nav"> 
                    <li>
                        <a href="#AddNewOrder">Add New Order</a>
                    </li>
                    <li>
                        <a href="#ShowOrder">Show Orders</a>
                    </li>
                </ul>
            </div>
            <div class="col-md-9">
                <div ng-view>

                </div>
            </div>
        </div>
    </div>

    <script src="Scripts/angular.js"></script>
    <script src="Scripts/angular-route.js"></script>
    <script src="script/app.js"></script>
</body>
</html>


/// <reference path="C:\Dev\WebApplication1\WebApplication1\Scripts/angular.js" />
var sampleApp = angular.module('sampleApp', ['ngRoute']);
//Define Routing 
sampleApp.config(function ($routeProvider) {
    $routeProvider.when('/AddNewOrder', {
        templateUrl: 'templates/AddNewOrder.html',
        controller: 'AddOrderController'
    }).when('/ShowOrder', {
        templateUrl: 'templates/ShowOrder.html',
        controller: 'ShowOrderController'
    });
});

sampleApp.controller('AddOrderController', function ($scope) {
    $scope.message = "This is Add Order screen";
});

sampleApp.controller('ShowOrderController', function ($scope) {
    $scope.message = "This is show Order screen";
});

Wrong URL is getting populated on browser addressbar when clicking on the AddNew Order link. http://localhost:63022/Sample1.html#!#AddNewOrder

Please suggest what is wrong here.. Routing is not working at all.

4
  • Please help here.. i am not able find why this routing is not working. Commented Feb 20, 2017 at 3:21
  • Don't prefix your href attributes with #. See the example here ~ docs.angularjs.org/api/ngRoute/service/$route#example Commented Feb 20, 2017 at 3:33
  • try ui-sref in place of href Commented Feb 20, 2017 at 4:09
  • @SaurabhAgrawal OP is using ngRoute, not ui.router Commented Feb 20, 2017 at 4:52

1 Answer 1

1

Your links are wrong. Try this:

            <li>
                <a href="#/AddNewOrder">Add New Order</a>
            </li>
            <li>
                <a href="#/ShowOrder">Show Orders</a>
            </li>

Or without #/ at all

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

5 Comments

<div class="container"> <div class="row"> <div class="col-md-3"> <ul class="nav"> <li> <a href="#/AddNewOrder">Add New Order</a> </li> <li> <a href="#/ShowOrder">Show Orders</a> </li> </ul> </div> <div class="col-md-9"> <div ng-view> </div> </div> </div> </div>
So it worked? Don't forget to flag the answer as correct if it did.
@Hemant try without the #/ prefix as shown in the example in the official documentation
Right, try it like I mentioned above (or without #/) <a href="AddNewOrder">Add New Order</a>
Also @Hermant, to make sure the routing is good, you can try changing the url manually to the expected url and checking if that works: localhost:63022/#/AddNewOrder

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.