AngularJS路由器配置不起作用

时间:2014-01-16 04:55:54

标签: javascript angularjs

我刚刚开始学习角度JS。 我为路由制作了一些测试代码。 但它似乎无法正常工作

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>

我在demoApp.config或控制器设置中遗漏了什么吗? 我也在javascript控制台中收到错误消息

未捕捉错误:[$ injector:modulerr] http://errors.angularjs.org/1.2.9/ $ injector / modulerr?p0 = demoApp&amp; p1 =错误%3A ... ogleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.9%2Fangular.min。 js%3A32%3A232)

3 个答案:

答案 0 :(得分:4)

您缺少ngRoute模块。您必须将angular-route.js文件添加到脚本中并加载模块,如下所示:

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

答案 1 :(得分:2)

添加了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 :(得分:0)

根据您的HTML,您缺少对应用程序模块的ngRoute模块的引用。如下所示

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