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