以下是演示angular路由切换的demo.

主页:index.html

<!doctype html>
<html ng-app="bookStoreApp">

<head>
    <meta charset="UTF-8">
    <title>BookStore</title>
    <script src="framework/1.3.0.14/angular.js"></script>
    <script src="framework/1.3.0.14/angular-route.js"></script>
    <script src="framework/1.3.0.14/angular-animate.js"></script>
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/filters.js"></script>
    <script src="js/services.js"></script>
    <script src="js/directives.js"></script>
</head>

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

</html>

模板文件(html碎片文件)tpls/:

hello.html:

<p>{{greeting.text}},Angular</p>

bookList.html:

<ul>
    <li ng-repeat="book in books">
        书名:{{book.title}}&nbsp;&nbsp;&nbsp;作者:{{book.author}}
    </li>
</ul>

控制器文件js/:

controllers.js:

var bookStoreCtrls = angular.module('bookStoreCtrls', []);

bookStoreCtrls.controller('HelloCtrl', ['$scope',
    function($scope) {
        $scope.greeting = {
            text: 'Hello'
        };
    }
]);

bookStoreCtrls.controller('BookListCtrl', ['$scope',
    function($scope) {
        $scope.books =[
         {title:"《AngularJS从入门到精通》",author:"中华烟云"},
         {title:"《AngularJS权威指南》",author:"中华烟云"},
         {title:"《用AngularJS开发下一代WEB应用》",author:"中华烟云"}
        ]
    }
]);

/**
 * 这里接着往下写,如果控制器的数量非常多,需要分给多个开发者,可以借助于grunt来合并代码
 */

最后实现路由功能的是app.js:

var bookStoreApp = angular.module('bookStoreApp', [
    'ngRoute', 'ngAnimate', 'bookStoreCtrls', 'bookStoreFilters',
    'bookStoreServices', 'bookStoreDirectives'             //[]内的为依赖注入的模块,ng开头的为angular自带的模块
]);

bookStoreApp.config(function($routeProvider) {         //$routeProvider是angular-route.js提供的原生路由对象,可以通过$routeProvider.when({}).when({})....otherwise({});这种链式写法来配置路由
    $routeProvider.when('/hello', {                              //'/hello'为路由路径,即在哈希符#后面动态输入的路径字串,如:....#/hello
        templateUrl: 'tpls/hello.html',                            //当路径为...#/hello时,调用模板文件'tpls/hello.html'
        controller: 'HelloCtrl'                                        //当路径为...#/hello时,调用控制器文件'js/HelloCtrl.js
    }).when('/list',{                                                  //:此时若切换到...#/list路径下
     templateUrl:'tpls/bookList.html',                          //当路径为...#/list时,调用模板文件'tpls/bookList.html'
     controller:'BookListCtrl'                                      //当路径为...#/list时,调用控制器文件'js/BookListCtrl.js
    }).otherwise({                                                   //:否则默认跳转到...#/hello路径下
        redirectTo: '/hello'
    })
});

注:angular-route.js提供的原生路由对象有个缺陷,就是不能深层次嵌套。要实现深层次嵌套,还得用UI-Router,源文件为angular-UI-Router.js.

UI-Router路由插件将在下一篇讲解.

--@angularJS--路由、模块、依赖注入的更多相关文章

  1. angular路由 模块 依赖注入

    1.模块 var helloModule=angular.module('helloAngular',[]); helloModule.controller('helloNgCrtl',['$scop ...

  2. 转: 理解AngularJS中的依赖注入

    理解AngularJS中的依赖注入 AngularJS中的依赖注入非常的有用,它同时也是我们能够轻松对组件进行测试的关键所在.在本文中我们将会解释AngularJS依赖注入系统是如何运行的. Prov ...

  3. 理解AngularJS中的依赖注入

    点击查看AngularJS系列目录 理解AngularJS中的依赖注入 AngularJS中的依赖注入非常的有用,它同时也是我们能够轻松对组件进行测试的关键所在.在本文中我们将会解释AngularJS ...

  4. 详解AngularJS中的依赖注入

    点击查看AngularJS系列目录 依赖注入 一般来说,一个对象只能通过三种方法来得到它的依赖项目: 我们可以在对象内部创建依赖项目 我们可以将依赖作为一个全局变量来进行查找或引用 我们可以将依赖传递 ...

  5. AngularJS学习之依赖注入

    1.什么是依赖注入:简称DI,是一种软件设计模式,在这种模式下,一个或更多的依赖(或服务)被注入(或者通过引用传递)到一个独立的对象(或客户端)中,然后成为了该客户端状态的一部分. 该模式分离了客户端 ...

  6. AngularJS中的依赖注入

    依赖注入 | Dependency Injection 原文链接: Angular Dependency Injection翻译人员: 铁锚翻译时间: 2014年02月10日说明: 译者认为,本文中所 ...

  7. AngularJS源码分析之依赖注入$injector

    开篇 随着javaEE的spring框架的兴起,依赖注入(IoC)的概念彻底深入人心,它彻底改变了我们的编码模式和思维.在IoC之前,我们在程序中需要创建一个对象很简单也很直接,就是在代码中new O ...

  8. 细数Javascript技术栈中的四种依赖注入

    作为面向对象编程中实现控制反转(Inversion of Control,下文称IoC)最常见的技术手段之一,依赖注入(Dependency Injection,下文称DI)可谓在OOP编程中大行其道 ...

  9. Angular依赖注入:全面讲解(翻译中)

    在实际使用Angular依赖注入系统时,你需要知道的一切都在本文中.我们将以实用易懂并附带示例的形式解释它的所有高级概念. Angular最强大.最独特的功能之一就是它内置的依赖注入系统. 大多数时候 ...

  10. 4.了解AngularJS模块和依赖注入

    1.模块和依赖注入概述 1.了解模块 AngularJS模块是一种容器,把代码隔离并组织成简洁,整齐,可复用的块. 模块本身不提供直接的功能:包含其他提供功能的对象的实例:控制器,过滤器,服务,动画 ...

随机推荐

  1. iptables-过滤61开头的ip数据包

    iptables -F root@android:/system/bin # iptables -L -n iptables -L -n Chain INPUT (policy ACCEPT) tar ...

  2. USACO Section 1.2 Dual Palindromes 解题报告

    题目 题目描述 有一些数(如 21),在十进制时不是回文数,但在其它进制(如二进制时为 10101)时就是回文数. 编一个程序,从文件读入两个十进制数N.S.然后找出前 N 个满足大于 S 且在两种以 ...

  3. GtkImageMenuItem

    做了个工具条,每次点arrow出来的菜单都没图标,郁闷;查来查去,看源码,看css,最后知道GtkAction缺省就是对应GtkImageMenuItem,再一试,跟toolbar无关,换menu也不 ...

  4. 织梦网站底部的Power by DedeCms怎么去掉?

    由于织梦DEDECMS程序6月份的漏洞,很多织梦网站都被黑了,所以大家都在抓紧时间更新系统补丁.但是这次的DEDECMS V5.7版本更新后,在前台网页底部会出现织梦版权信息 “powered by ...

  5. cnn 文章

    http://www.cnblogs.com/fengfenggirl/p/cnn_implement.html http://www.2cto.com/kf/201603/493553.html h ...

  6. Quick Cocos2dx controller的初步实现

    很久没有记笔记了,今天记一下,最近都在瞎忙活,都不知道自己干了些啥. 我的Controller是在官方的mvc sample的里面的PlayerDualController上更改的,所以很多地方还没来 ...

  7. CodeForces 620A Professor GukiZ's Robot

    水题 #include<cstdio> #include<cstring> #include<cmath> #include<stack> #inclu ...

  8. (中等) POJ 2886 Who Gets the Most Candies? , 反素数+线段树。

    Description N children are sitting in a circle to play a game. The children are numbered from 1 to N ...

  9. Spring自学教程-注解的使用(三)

    一.java中的注解 定义注解 下面是一个定义注解的实例. @Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documente ...

  10. MySQL的char和varchar

    一.VARCHAR与CHAR字符型数据的差异 在MySQL数据库中,用的最多的字符型数据类型就是Varchar和Char,这两种数据类型虽然都是用来存放字符型数据,但是无论从结构还是从数据的保存方式来 ...