转自:https://www.cnblogs.com/best/tag/Angular/

O'Reilly书上的伪代码

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

someModule.config(function($routeProvider){
$routeProvider
.when('url',{controller:aController, templateUrl:'/path/to/template'})
.when(...)//other mappings for your app
.otherwise(...)//what to do if nothing else matches
});

$route被用于URLS到controller和view(HTML模板)之间的链接,它会监控$location.url()并试图对此路径及对应的路由配置进行映射,使用时需要注入安装ngroute模块。

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

举个详细的栗子

var app = angular.module('app', ['ngRoute']);
app.config(function ($routeProvider){
$routeProvider
.when('/other', {
controller: 'otherCtrl',
templateUrl: 'content/views/other.html',
publicAccess: true
})
.when('/', {
controller: 'homeCtrl',
templateUrl: 'content/views/home.html'
})
.when('/other/:id', {
controller: 'otherDetailCtrl',
templateUrl: 'content/views/otherDetail.html',
publicAccess: true
})
.otherwise({
redirectTo: '/'
});
} app.controller('homeCtrl',function($scope,$http){
console.log('i am home page');
$scope.title = 'i am home page';
}); app.controller('otherCtrl',function($scope){
console.log('i am other page');
$scope.title = 'i am other page';
}); app.controller('otherDetailCtrl',function($scope, $routeParams, $location){
var id = $routeParams.id;
if(id == 0) {
$location.path('/other');
}
console.log('i am otherDetailCtrl page');
$scope.title = 'i am otherDetailCtrl page';
});

在$route(路由)中,提供了两个依赖性服务:$location、$routeParams。

$location、$routeParams均可在controller中使用,如上otherDetailCtrl中,可通过$routeParams获取路由时所传参数,可通过$location进行路由跳转。

最新文章

  1. Java虚拟机浅探
  2. 《BI项目笔记》增量ETL数据抽取的策略及方法
  3. margin:0 auto在IE中失效的解决方案
  4. JavaWeb学习记录(十九)——jstl自定义标签之简单标签
  5. python练习程序(显示图像)
  6. Eclipse 安装Activiti 插件失败解决方法
  7. PE基金的运作模式有哪些?
  8. Asp.Net MVC5入门学习系列①
  9. centos7救援模式--误删/usr/bin/恢复
  10. python中用修饰器进行异常日志记录
  11. vue线上项目,优化前后对比
  12. libco协程库上下文切换原理详解
  13. Inno Setup入门(八)——有选择性的安装文件
  14. repo sync出现“fatal: '../platform/abi/cpp.git' does not appear to be a git repository”的解决方案
  15. Spark学习笔记4:数据读取与保存
  16. Java并发编程--1.Thread和Runnable
  17. angularJS1笔记-(4)-自定义服务
  18. JAVA技术分享:消失的线程
  19. OpenCV学习笔记 - Video Analysis - 录制视频
  20. 如何从官网开始 mongo java

热门文章

  1. Google笔试(2015年8月)
  2. Java Bean 简单介绍及其应用
  3. lvs中dr模式配置脚本
  4. android 自己定义dialog并实现失去焦点(背景透明)的功能
  5. django 笔记5 外键 ForeignKey
  6. centos7 阿里云yum源更换
  7. json的认识及对json数据的相互转化
  8. Liunx-php7安装swoole扩展
  9. ubuntu12.04
  10. PostgreSQL Replication之第六章 监控您的设置(2)