AngularJS路由系列(5)-- UI-Router的路由约束、Resolve属性、路由附加数据、路由进入退出事件
本系列探寻AngularJS的路由机制,在WebStorm下开发。主要包括:
● UI-Router约束路由参数
● UI-Router的Resolve属性
● UI-Router给路由附加数据
● UI-Router的onEnter和onExit事件
AngularJS路由系列包括:
1、AngularJS路由系列(1)--基本路由配置
2、AngularJS路由系列(2)--刷新、查看路由,路由事件和URL格式,获取路由参数,路由的Resolve
3、AngularJS路由系列(3)-- UI-Router初体验
4、AngularJS路由系列(4)-- UI-Router的$state服务、路由事件、获取路由参数
5、AngularJS路由系列(5)-- UI-Router的路由约束、Resolve属性、路由附加数据、路由进入退出事件
6、AngularJS路由系列(6)-- UI-Router的嵌套State
项目文件结构
node_modules/
public/
.....app/
..........bower_components/
...............toastr/
....................toastr.min.css
....................toastr.min.js
...............jquery/
....................dist/
.........................jquery.min.js
...............angular/
....................angular.min.js
...............angular-ui-router/
....................release/
.........................angular-ui-router.min.js
...............angular-route/
.........................angular-route.min.js
..........controllers/
...............HomeController.js
...............AllSchoolsController.js
...............AllClassroomsController.js
...............AllActivityiesController.js
...............ClassroomController.js
...............ClassroomSummaryController.js
...............ClassroomMessageController.js
..........css/
...............bootstrap.cerulean.min.css
..........filters/
...............activityMonthFilter.js
..........services/
...............dataServices.js
...............notifier.js
..........templates/
...............home.html
...............allSchools.html
...............allClassrooms.html
...............allActivities.html
...............classroom.html
...............classroomDetail.html
...............classroom_parent.html
..........app.js
.....index.html
.....favicon.ico
server/
.....data/
.....routes/
.....views/
.....helpers.js
package.json
server.js
UI-Router约束路由参数
■ app.js, 约束参数
(function(){
var app = angular.module('app',['ui.router']); app.config(['$logProvider', '$stateProvider', function($logProvider, $stateProvider){
$logProvider.debugEnabled(true); $stateProvider
.state('home',{
url: '/',
templateUrl: '/app/templates/home.html',
controller: 'HomeController', //也可以写成HomeController as home
controllerAs: 'home'
})
.state('schools',{
url: '/schools',
controller: 'AllSchoolController',
controllerAs: 'schools',
templateUrl: '/app/templates/allSchools.thml'
})
.state('classrooms',{
url:'/classrooms',
controller: 'AllClassroomsController',
controllerAs: 'classrooms',
templateUrl: '/app/tempates/allClassrooms.html'
})
.state('activities', {
url: '/activities',
controller: 'AllActivitiesController',
controllerAs: 'activities',
templateUrl: '/app/templates/allActivities.html'
})
.state('classroom_summary', {
url: '/classrooms/:id',
templateUrl: '/app/templates/classroom.html',
controller: 'ClassroomController',
controllerAs: 'classroom'
})
.state('classroom_detail',{
url: '/classrooms/{id: [0-9]}/detail/{month}',
templateUrl: '/app/templates/classroomDetail.html',
controller: 'ClassroomController',
controllerAs: 'classroom'
})
}]); app.run(['$rootScope', '$log', function($rootScope, $log){
$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){
$log.debug('successfully changed states') ; $log.debug('event', event);
$log.debug('toState', toState);
$log.debug('toParams', toParams);
$log.debug('fromState', fromState);
$log.debug('fromParams', fromParams);
}); $rootScope.$on('$stateNotFound', function(event, unfoundState, fromState, fromParams){
$log.error('The request state was not found: ' + unfoundState);
}); $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error){
$log.error('An error occurred while changing states: ' + error); $log.debug('event', event);
$log.debug('toState', toState);
$log.debug('toParams', toParams);
$log.debug('fromState', fromState);
$log.debug('fromParams', fromParams);
});
}]);
}());
url: '/classrooms/{id: [0-9]}/detail/{month}'这个就对id这个路由参数进行了约束。
■ app.js, 路由参数不符合约束的解决办法
UI-Router为我们提供了$urlRouteProvider服务。
(function(){
var app = angular.module('app',['ui.router']); app.config(['$logProvider', '$stateProvider', '$urlRouteProvider', function($logProvider, $stateProvider, $urlRouteProvider){
$logProvider.debugEnabled(true); //解决路由异常的办法在这里
$urlRouteProvider.otherwise('/'); $stateProvider
.state('home',{
url: '/',
templateUrl: '/app/templates/home.html',
controller: 'HomeController', //也可以写成HomeController as home
controllerAs: 'home'
})
.state('schools',{
url: '/schools',
controller: 'AllSchoolController',
controllerAs: 'schools',
templateUrl: '/app/templates/allSchools.thml'
})
.state('classrooms',{
url:'/classrooms',
controller: 'AllClassroomsController',
controllerAs: 'classrooms',
templateUrl: '/app/tempates/allClassrooms.html'
})
.state('activities', {
url: '/activities',
controller: 'AllActivitiesController',
controllerAs: 'activities',
templateUrl: '/app/templates/allActivities.html'
})
.state('classroom_summary', {
url: '/classrooms/:id',
templateUrl: '/app/templates/classroom.html',
controller: 'ClassroomController',
controllerAs: 'classroom'
})
.state('classroom_detail',{
url: '/classrooms/{id: [0-9]}/detail/{month}',
templateUrl: '/app/templates/classroomDetail.html',
controller: 'ClassroomController',
controllerAs: 'classroom'
})
}]); app.run(['$rootScope', '$log', function($rootScope, $log){
$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){
$log.debug('successfully changed states') ; $log.debug('event', event);
$log.debug('toState', toState);
$log.debug('toParams', toParams);
$log.debug('fromState', fromState);
$log.debug('fromParams', fromParams);
}); $rootScope.$on('$stateNotFound', function(event, unfoundState, fromState, fromParams){
$log.error('The request state was not found: ' + unfoundState);
}); $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error){
$log.error('An error occurred while changing states: ' + error); $log.debug('event', event);
$log.debug('toState', toState);
$log.debug('toParams', toParams);
$log.debug('fromState', fromState);
$log.debug('fromParams', fromParams);
});
}]);
}());
■ UI-Router的params属性设置路由参数
(function(){
var app = angular.module('app',['ui.router']); app.config(['$logProvider', '$stateProvider', '$urlRouteProvider', function($logProvider, $stateProvider, $urlRouteProvider){
$logProvider.debugEnabled(true); //解决路由异常的办法在这里
$urlRouteProvider.otherwise('/'); $stateProvider
.state('home',{
url: '/',
templateUrl: '/app/templates/home.html',
controller: 'HomeController', //也可以写成HomeController as home
controllerAs: 'home'
})
.state('schools',{
url: '/schools',
controller: 'AllSchoolController',
controllerAs: 'schools',
templateUrl: '/app/templates/allSchools.thml'
})
.state('classrooms',{
url:'/classrooms',
controller: 'AllClassroomsController',
controllerAs: 'classrooms',
templateUrl: '/app/tempates/allClassrooms.html'
})
.state('activities', {
url: '/activities',
controller: 'AllActivitiesController',
controllerAs: 'activities',
templateUrl: '/app/templates/allActivities.html'
})
.state('classroom_summary', {
url: '/classrooms/:id',
templateUrl: '/app/templates/classroom.html',
controller: 'ClassroomController',
controllerAs: 'classroom'
})
.state('classroom_detail',{
url: '/classrooms/{id: [0-9]}/detail/{month}',
templateUrl: '/app/templates/classroomDetail.html',
controller: 'ClassroomController',
controllerAs: 'classroom',
params: {
classroomMessage: { value: 'Learning is fun!'}
}
})
}]); app.run(['$rootScope', '$log', function($rootScope, $log){
$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){
$log.debug('successfully changed states') ; $log.debug('event', event);
$log.debug('toState', toState);
$log.debug('toParams', toParams);
$log.debug('fromState', fromState);
$log.debug('fromParams', fromParams);
}); $rootScope.$on('$stateNotFound', function(event, unfoundState, fromState, fromParams){
$log.error('The request state was not found: ' + unfoundState);
}); $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error){
$log.error('An error occurred while changing states: ' + error); $log.debug('event', event);
$log.debug('toState', toState);
$log.debug('toParams', toParams);
$log.debug('fromState', fromState);
$log.debug('fromParams', fromParams);
});
}]);
}());
■ ClassroomController.js, 接受更多的路由参数
(function(){
angular.module('app')
.controller('ClassroomController',['dataService', 'notifier', '$stateParams', ClassroomController]); function ClassroomController(dataService, notifier, $stateParams){
var vm = this; vm.month = $stateParams.month; //接受param设置的参数
vm.message = $stateParams.classroomMessage; dataService.getClassroom($stateParams.id)
.then(function(classroom){
vm.currentClassroom = classroom; if($stateParams.month){
if(classroom.activities.length > 0){
vm.timePeriod = dataService.getMonthName($stateParams.month);
} else {
vm.timePeriod = 'No activities this month';
}
}
else{
vm.timePeriod = 'All activities';
}
})
.catch(showError); function showError(message){
notifier.error(message);
} }
}());
■ classroomDetal.html
{{classroom.message}}
UI-Router的Resolve属性
■ resolve 属性
.state('activities',{
url: '/activities',
controller: 'AllAcivitiesController',
controllerAs: 'activities',
templateUrl: '/app/templates/allActivities.html',
resolve: {
activities: function(dataService){
return dataService.getAllActiviites();
}
}
})
● 可以被注入到controller中去
● 返回一个object对象,对象的属性自定义,属性值是一个返回promise的函数
● promises必须被resolve,在变化发生之前
■ app.js, 添加resolve属性
.state('activities',{
url: '/activities',
controller: 'AlLActivitiesController',
controllerAs: 'activities',
templateUrl: '/app/templates/allActivities.html',
resolve: {
activities: function(dataService){
return dataService.getAllActivites();
}
}
})
■ AllActivitiesController.js,从resolve中获取数据
(function(){
angular.module('app')
.controller('AllActivitiesController',['dataService','notifier','$state','activites', AllActivitiesController]); function AllActivitiesController(dataService, notifier, $state, activities){
var vm = this; vm.selectedMonth = 1;
vm.allActivities = activities; vm.search = function(){
$state.go('classroom_detail',{id:vm.selectedClassroom.id, month: vm.selectedMonth});
} dataService.getAllClassrooms()
.then(function(classroom){
vm.allClassrooms = classrooms;
vm.selectedClassroom = classrooms[0];
})
.catch(showError); function showError(message){
notifier.error(message);
}
}
}());
当点击Activites的时候,数据已经在controller,不需要重新获取,大大减少了页面加载时间。
UI-Router给路由附加数据
■ 给states附加数据
.state('activities',{
url: '/activities',
controller: 'AllActivitesController',
templateUlr: '/app/templates/allActivities.html',
data: {
name: 'MyActivity',
desc: 'Fun!'
}
})
● data中定义的属性是任意的
● 能被子state继承
■ app.js, 添加附加数据
.state('activities',{
url: '/activities',
controller: 'AlLActivitiesController',
controllerAs: 'activities',
templateUrl: '/app/templates/allActivities.html',
resolve: {
activities: function(dataService){
return dataService.getAllActivites();
}
},
data: {
name: 'My Activity',
desc: 'Func!'
},
foo: {
myFoo: 'bar'
}
})
■ AllActivitiesController.js,从resolve中获取数据
(function(){
angular.module('app')
.controller('AllActivitiesController',['dataService','notifier','$state','activites','$log', AllActivitiesController]); function AllActivitiesController(dataService, notifier, $state, activities, $log){
var vm = this; vm.selectedMonth = 1;
vm.allActivities = activities; $log.debug($state.current.data);
$log.debug($state.current.foo); vm.search = function(){
$state.go('classroom_detail',{id:vm.selectedClassroom.id, month: vm.selectedMonth});
} dataService.getAllClassrooms()
.then(function(classroom){
vm.allClassrooms = classrooms;
vm.selectedClassroom = classrooms[0];
})
.catch(showError); function showError(message){
notifier.error(message);
}
}
}());
UI-Router的onEnter和onExit事件
.state('classroom',{
url:'/clsssrooms',
controller:'AllClssroomsController',
controllerAs: 'classrooms',
templateUrl: '/app/tempaltes/allClassrooms.html',
onEnter: function($log){
$log.debug('Entering the classrooms state');
},
onExit: function($log){
$log.debug('Existing the classrooms state');
}
})
未完待续~~
AngularJS路由系列(5)-- UI-Router的路由约束、Resolve属性、路由附加数据、路由进入退出事件的更多相关文章
- AngularJS 使用 UI Router 实现表单向导
Today we will be using AngularJS and the great UI Router and the Angular ngAnimate module to create ...
- [转]AngularJS 使用 UI Router 实现表单向导
本文转自:http://www.oschina.net/translate/angularjs-multi-step-form-using-ui-router 今天我们将使用AngularJs和伟大的 ...
- ngRoute 和 ui.router 的使用方法和区别
在单页面应用中要把各个分散的视图给组织起来是通过路由机制来实现的.本文主要对 AngularJS 原生的 ngRoute 路由模块和第三方路由模块 ui.router 的用法进行简单介绍,并做一个对比 ...
- react第十二单元(react路由-使用react-router-dom-认识相关的组件以及组件属性)
第十二单元(react路由-使用react-router-dom-认识相关的组件以及组件属性) #课程目标 理解路由的原理及应运 理解react-router-dom以及内置的一些组件 合理应用内置组 ...
- AngularJS路由系列(6)-- UI-Router的嵌套State
本系列探寻AngularJS的路由机制,在WebStorm下开发.本篇主要涉及UI-Route的嵌套State. 假设一个主视图上有两个部分视图,部分视图1和部分视图2,主视图对应着一个state,两 ...
- AngularJS路由系列(4)-- UI-Router的$state服务、路由事件、获取路由参数
本系列探寻AngularJS的路由机制,在WebStorm下开发.主要包括: ● UI-Router的$state服务● UI-Router的路由事件● UI-Router获取路由参数 Angular ...
- AngularJS路由系列(3)-- UI-Router初体验
本系列探寻AngularJS的路由机制,在WebStorm下开发. AngularJS路由系列包括: 1.AngularJS路由系列(1)--基本路由配置2.AngularJS路由系列(2)--刷新. ...
- AngularJS路由系列(2)--刷新、查看路由,路由事件和URL格式,获取路由参数,路由的Resolve
本系列探寻AngularJS的路由机制,在WebStorm下开发.主要包括: ● 刷新路由● 查看当前路由以及所有路由● 路由触发事件● 获取路由参数 ● 路由的resolve属性● 路由URL格式 ...
- AngularJS路由系列(1)--基本路由配置
本系列探寻AngularJS的路由机制,在WebStorm下开发.主要包括: ● 路由的Big Picture ● $routeProvider配置路由 ● 使用template属性 ● 使用temp ...
随机推荐
- python3之SQLAlchemy
1.SQLAlchemy介绍 SQLAlchemy是Python SQL工具包和对象关系映射器,为应用程序开发人员提供了SQL的全部功能和灵活性. 它提供了一整套众所周知的企业级持久性模式,专为高效和 ...
- Palindrome Partitioning I & II
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Python_oldboy_自动化运维之路_面向对象2(十)
本节内容: 面向对象程序设计的由来 什么是面向对象的程序设计及为什么要有它 类和对象 继承与派生 多的态与多态性 封装 静态方法和类方法 面向对象的软件开发 反射 类的特殊成员方法 异常处理 1.面向 ...
- XSS代码注入框架
首先需要了解一下几点: 1.浏览器中Javascript变量的生命周期 Javascript变量的生命周期并不是你声明这个变量个窗口闭就被回收,只要有引用就会一直持续到浏览器关闭. 2.window对 ...
- 卷积神经网络CNN经典模型整理Lenet,Alexnet,Googlenet,VGG,Deep Residual Learning(转)
参考:http://blog.csdn.net/xbinworld/article/details/45619685
- python学习之算法、自定义模块、系统标准模块(上)
算法.自定义模块.系统标准模块(time .datetime .random .OS .sys .hashlib .json和pickle) 一:算法回顾: 冒泡算法,也叫冒泡排序,其特点如下: 1. ...
- poj 1797 一条路径中的最小边 再找出最大的
Sample Input 1 // T3 3// n m1 2 3//u v w1 3 42 3 5Sample Output Scenario #1:4 # include <iostream ...
- 电脑同时安装Python2和Python3以及virtualenvwrapper(转)
电脑同时安装Python2和Python3以及virtualenvwrapper https://www.jianshu.com/p/d22f19496e03 windows: 1 下载地址:P ...
- sublime安装package control 的方法
1.安装sublime3后,点击菜单 view>show console,复制以下代码按回车即可,安装时稍微有些卡,需要耐心等待. import urllib.request,os; pf = ...
- InnoDB的锁机制浅析(五)—死锁场景(Insert死锁)
可能的死锁场景 文章总共分为五个部分: InnoDB的锁机制浅析(一)-基本概念/兼容矩阵 InnoDB的锁机制浅析(二)-探索InnoDB中的锁(Record锁/Gap锁/Next-key锁/插入意 ...