AngularJS router configuration not working

I just started learning angular JS. I made some test codes for routing. But it doesn't seem to work

demoApp.js

var demoApp = angular.module('demoApp',[])

demoApp.config(function($routeProvider){
    $routeProvider
        .when('/view1',
            {   
                controller:'SimpleController'
                ,templateUrl : 'View1.html'
            })
        .when('/view2',
            {
                controller:'SimpleController'
                ,templateUrl : 'View2.html'
            })
        .otherwise({redirectTo:'/view1'});  

});

demoApp.controller('SimpleController',function($scope){
        $scope.customers = [
                {name:'Terry.Cho',city:'Seoul'},
                {name:'Cath',city:'Suwon'},
                {name:'Carry',city:'Suwon'}
        ];
        alert('hello controller');
    } );


alert("hello");

home.html

<html ng-app="demoApp">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js"></script>
    <script src="demoApp.js"></script>
</head>
<body>
    <a href="#/view1">View1</a>
    <a href="#/view2">View2</a>

    <div>
        <div ng-view></div>
    </div>
</body>
</html>

view1.html

<div class="container">
    <h2> View 1 </h2>

view2.html

<div class="container">
    <h2> View 2 </h2>

Am I missing something in the demoApp.config or controller settings? I also got an error in javascript console

Unprepared error: [$ injector: modulerr] http://errors.angularjs.org/1.2.9/ $ injector / modulator? p0 = demoApp & p1 = Error% 3A ... ogleapis.com% 2Fajax% 2Flibs% 2Fangularjs% 2F1.2.9% 2Fangular.min.js% 3A32% 3A232)

+4
source share
3 answers

ngRoute. angular-route.js :

 angular.module('demoApp', ['ngRoute']);
+4

demo.js

var demoApp = angular.module('demoApp',['ngRoute'])

home.html

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular-route.min.js
+2

according to yours you HTMLwill not have a link to a module ngRoutein your application module. as below

var demo = angular.module('demoapp',['ngRoute']);
0
source

All Articles