AngularJS路由器配置不工作 [英] AngularJS router config is not working

查看:140
本文介绍了AngularJS路由器配置不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学的角度JS。
我做了一些测试code路由。
但似乎不工作

I'm just started to learn angular JS. I made some test code for routing. But it seems not working

demoApp.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的

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

view1.html

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

view2.html

view2.html

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

我有没有缺少demoApp.config或控制器设置的东西吗?
我在JavaScript控制台也得到了错误信息

did i missing something in demoApp.config or controller setting? i also got error message in javascript console

未捕获的错误:[$喷油器:modulerr]的http://errors.angularjs.org/1.2.9/$injector/modulerr?p0=demoApp&p1=Error%3A…ogleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.9%2Fangular.min.js%3A32%3A232)

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

推荐答案

您缺少 ngRoute 模块。你必须角route.js 文件添加到您的脚本和加载模块是这样的:

You are missing the ngRoute module. You have to add angular-route.js file to your scripts and load the module like this:

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

这篇关于AngularJS路由器配置不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆