Babu
Babu

Reputation: 137

How to do routing in angular js?

I am trying to do angular js routing application its works fine but i have facing an error that was i am clicking [View Students List] that is not working and page is not navigating to another page..

below is my code.. main.js

var app = angular.module("mainApp", ['ngRoute']);

app.config(['$routeProvider','$locationProvider',
    function($routeProvider,$locationProvider){
    $routeProvider
        .when('/', {
            templateUrl: 'home.html',
            controller: 'StudentController'
        })
        .when('/viewStudents', {
            templateUrl: 'viewStudents.html',
            controller: 'StudentController'
        })
        .otherwise({
            redirectTo: '/'
        });
        $locationProvider.hashPrefix('');
        $locationProvider.html5Mode({
            enabled:true,
            requiredBase:false
        }); 
}]);

app.controller('StudentController', function($scope) {
    $scope.students = [
        {name: 'Mark Waugh', city:'New York'},
        {name: 'Steve Jonathan', city:'London'},
        {name: 'John Marcus', city:'Paris'}
    ];

    $scope.message = "Click on the hyper link to view the students list.";
});

below is my home.html

<div class="container">
    <h2> Welcome </h2>
    <p>{{message}}</p>
    <a ng-href="/viewStudents"> View Students List</a>
</div>

below is my viewStudents.html

<div class="container">
    <h2> View Students </h2>
    Search:
    <br/>
        <input type="text" ng-model="name" />
    <br/>
    <ul>
        <li ng-repeat="student in students | filter:name">{{student.name}} , {{student.city}}</li>
    </ul>
    <a ng-href="/home">Back</a>
</div>

below is my index.html code

<!DOCTYPE html>
<html>
    <head>
        <base href="/" />
        <script src="js/angular.min.js"></script>
        <script src="js/angular.route.js"></script>
        <script src="js/main.js"></script>
    </head>
        <body ng-app="mainApp">
            <ng-view></ng-view>
        </body>
</html>

Upvotes: 1

Views: 73

Answers (3)

AdityaParab
AdityaParab

Reputation: 7100

If you want to enable html5 mode, make sure you include your base tag AFTER your scripts are loaded. Like this

<!DOCTYPE html>
<html>
    <head>
        <script src="js/angular.min.js"></script>
        <script src="js/angular.route.js"></script>
        <script src="js/main.js"></script>
        <base href="/" />
    </head>
        <body ng-app="mainApp">
            <ng-view></ng-view>
        </body>
</html>

And it should work fine.

Take a look at working plunk

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222722

You need to change the Home.html as,

<div class="container">
    <h2> Welcome </h2>
    <p>{{message}}</p>
    <a ng-href="#/viewStudents"> View Students List</a>  
</div>

DEMO

Upvotes: 1

mt eee
mt eee

Reputation: 90

change

<a ng-href="/viewStudents"> View Students List</a>

to

<a ng-href="#/viewStudents"> View Students List</a>  

try it

Upvotes: 1

Related Questions