Siva
Siva

Reputation: 3327

Angular JS routing not working

While i am trying to click on show its not showing any thing, i have wasted lot of time on this, but no luck can some one help.

Files....

  1. app.js
  2. controller.js
  3. index.html
  4. show.html

index.html

<html ng-app='Java4sApp'>
<head>
    <title>Angular Js Tutorials _ Java4s</title>
    <script type="text/javascript" src="angular.min.js"></script>
    <script type="text/javascript" src="app.js"></script>
    <script type="text/javascript" src="controllers.js"></script>
    </head>
<body>

<div ng-controller='Java4sController'>



            Settings :
            <a href="#add">Add Details</a> | <a href="#show">Show Details</a> 

            <div ng-view></div>

</div>

</body>
</html>

show.html

<html ng-app='Java4sApp'>
<head>
    <title>Angular Js Tutorials _ Java4s</title>
    <script type="text/javascript" src="angular.min.js"></script>
    <script type="text/javascript" src="app.js"></script>
    <script type="text/javascript" src="controllers.js"></script>
</head>
<body>

<div ng-controller='Java4sController'>


            <input type="text" ng-model="myModel" />

            <ul>
                <li ng-repeat='person in people | filter:myModel'>{{person.name}}</li>
            </ul>


</div>

</body>
</html>

controller.js

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

    $routeProvider.
      when('/add', {
        controller: 'Java4sController',
        templateUrl: '/add.html'
    }).
      when('/show', {
        controller: 'Java4sController',
        templateUrl: '/show.html'
    }).
      otherwise({
        redirectTo: '/index.html'
      });
}]);

app.controller("Java4sController",function($scope){

        $scope.people = [
            {name: 'Siva', city: 'Cary'},
            {name: 'Teja', city: 'Raleigh'},
            {name: 'Reddy', city: 'Durham'},
            {name: 'Venky', city: 'Denver'},
            {name: 'Arun', city: 'Texas'}
    ];

});

app.js

var app = angular.module("Java4sApp",[]);

Thank you.

Upvotes: 3

Views: 15423

Answers (1)

Tharaka
Tharaka

Reputation: 2395

You need to inject the 'ngRoute' dependency when you are creating your module.

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

Also make sure you obtain Angular routing js and add a reference to it, in your index.html

<script src="scripts/angular-route.js"></script>

A good example can be found here

Please let me know if this doesn't fix your problem

UPDATE

This is a simple plunker that shows AngularJS Routing

Upvotes: 8

Related Questions