使用时需要ui中用ui-view指令指定 如:

<div ui-view></div>

首先配置注册 ui-route

var mainModule = angular.module('main', ['ui.router']);

由于run方法是在angular启动的时候就会执行的,可以将路由跳转控制放到run方法中,比如某种条件下禁止路由跳转 另外全局事件也可以放到run方法中

mainModule.run(function($rootScope,$state,$http,$stateParams){
//这里把$state和$stateParams这两个对象放到$rootScope上,方便其它地方引用和注入。
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
$rootScope.$on('$stateChangeStart', function(event, toState,
toParams, fromState, fromParams) {
if (toState.name === "yxlpm") {
// 这里加入调出影响力排名页面是做出的判断
//使用jquery不使用$http是应为$http的请求是不定时的,等请求完成页面已经完成了跳转,evet事件就无效
// 如果好友公司数少于5个侧不显示页面,跳出提示
$.ajax({
method : 'POST',
url : '../userInfo/influence',
async: false,
headers : {
'token' : $rootScope.token
}
}).success(
function(resp, status, headers, config) {
if (resp.code === 8037) {
// $state.go('wo'); 路由跳转go方式
event.preventDefault();// 取消默认跳转行为
alert('您的影响力不足无法查看'); }
});
} });
})

基本路由配置

mainModule.config(function($stateProvider, $urlRouterProvider) {
// $urlRouterProvider.otherwise('../dongtai/smdt.html');
// //在配置(状态配置和when()方法)中没有找到url的任何匹配 $stateProvider.state('news', {
url : '/news/:type', // 消息 type为参数类型 取参数可用$stateParams.type
templateUrl : '../grsz/news.html'
}).state('sousuo.zrssjg', {
url : '/zrssjg/:topic', // 找人结果
templateUrl : '../sousuo/zrssjg.html'
}).state('zwxq', { //注意这边嵌套视图的写法
url : '/zwxq/:id', // 职位详情
views : {
'view1' : {
templateUrl : '../wo/zwxq.html'
},
‘view2’:{
templateUrl : '../wo/zlxq.html'
}
}
})
}); html:
//传参方式1、/news/1 反斜杆后面为参数
<a class="menu-item" href="#/news/1" hideblock>
消息
</a>
//传参方式2、topic为参数名 用.号来控制sousuo页面下的子页面
<a class="tag" ui-sref="sousuo.zrssjg({topic:ChildrenPosition.name})"</a> //指定路由
<div ui-view="view1"></div>
<div ui-view="view2"></div>

state事件

    //状态改变之前触发
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){ ... }) $rootScope.$on('$stateNotFound', function(event, unfoundState, fromState, fromParams){ ... })
//example
// somewhere, assume lazy.state has not been defined
$state.go("lazy.state", {a:1, b:2}, {inherit:false}); // somewhere else
$rootScope.$on('$stateNotFound',
function(event, unfoundState, fromState, fromParams){
console.log(unfoundState.to); // "lazy.state"
console.log(unfoundState.toParams); // {a:1, b:2}
console.log(unfoundState.options); // {inherit:false} + default options
})
//状态改变成功之后触发
$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){ ... }) $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error){ ... })

view事件

    View被加载但是DOM树构建之前时: 

    $scope.$on('$viewContentLoading', function(event, viewConfig){ ... }); 

    View被加载而且DOM树构建完成时: 

    $scope.$on('$viewContentLoaded', function(event){ ... });

另一种传参方式

    $state.go('sousuo.dtssjg', {topic : $scope.keyWord}, {reload : true});
由于html中无法动态绑定ui-sref中的路径,可以在控制器中通过state来做跳转

路由中的其他配额

    templateProvider:返回HTML字符串的函数
$stateProvider.state(‘blog.detail', {
templateProvider: function ($timeout, $stateParams) {
return $timeout(function () {
return '<h1>' + $stateParams.blogID + '</h1>'
}, 100);
}
}) //以下几个项目中并没有进行配置,而是将功能分化到对控制器和指令中,具体功能也不太理解。可参照官方文档:https://github.com/angular-ui/ui-router/wiki
controller、controllerProvider:指定任何已经被注册的控制器或者一个作为控制器的函数 resolve:在路由到达前预载入一系列依赖或者数据,然后注入到控制器中。 data:数据不会被注入到控制器中,用途是从父状态传递数据到子状态。 onEnter/onExit:进入或者离开当前状态的视图时会调用这两个函数 关于angulsrjs入门介绍,可参阅这篇博文:http://www.zouyesheng.com/angular.html#toc66

ui-router的使用的更多相关文章

  1. 【原创】ui.router源码解析

    Angular系列文章之angular路由 路由(route),几乎所有的MVC(VM)框架都应该具有的特性,因为它是前端构建单页面应用(SPA)必不可少的组成部分. 那么,对于angular而言,它 ...

  2. angular : $location & $state(UI router)的关系

    次序:angular 的 location会先跑 $rootScope.$on("$locationChangeStart", function (scope, newUrl, o ...

  3. angularjs ngRoute和ui.router对比

    ngRoute模块是angularjs自带的路由模块,ui.router是一个第三方路由模块,接下来将对两者进行一个对比: ng-router(angular-router.js) ng-view n ...

  4. angular ui.router 路由传参数

    angular已经用了一段时间了,最近在做路由,做一下笔记. 路由跳转的时候进行穿参 ui.router方式 <a ui-sref="edit({id:5})"> 编辑 ...

  5. ngRoute 与ui.router区别

    angular路由 路由 (route) ,几乎所有的 MVC(VM) 框架都应该具有的特性,因为它是前端构建单页面应用 (SPA) 必不可少的组成部分. 那么,对于 angular 而言,它自然也有 ...

  6. AngularJS 使用 UI Router 实现表单向导

    Today we will be using AngularJS and the great UI Router and the Angular ngAnimate module to create ...

  7. ngRoute 和 ui.router 的使用方法和区别

    在单页面应用中要把各个分散的视图给组织起来是通过路由机制来实现的.本文主要对 AngularJS 原生的 ngRoute 路由模块和第三方路由模块 ui.router 的用法进行简单介绍,并做一个对比 ...

  8. [转]AngularJS 使用 UI Router 实现表单向导

    本文转自:http://www.oschina.net/translate/angularjs-multi-step-form-using-ui-router 今天我们将使用AngularJs和伟大的 ...

  9. [转]AngularJS+UI Router(1) 多步表单

    本文转自:https://www.zybuluo.com/dreamapplehappy/note/54448 多步表单的实现   在线demo演示地址https://rawgit.com/dream ...

  10. angularJS ui router 多视图单独刷新问题

    场景:视图层级如下 view1 --view11 --view111 需求:view11的一个动作过后,单独刷新view12 解决方式:修改层级设计 view1 --view11 --view111 ...

随机推荐

  1. 通过硬件层提高Android动画的性能

    曾有许多人问我为什么在他们开发的应用中,动画的性能表现都很差.对于这类问题,我往往会问他们:你们有尝试过在硬件层解决动画的性能问题么? 我们都知道,在播放动画的过程中View在每一帧动画的显示时重绘自 ...

  2. logstahs 匹配isslog

    2016-11-30 06:33:33 192.168.5.116 GET /Hotel/HotelDisplay/cncqcqb230 - 80 - 192.168.9.2 Mozilla/5.0+ ...

  3. bzoj1858

    比较烦的线段树 首先询问3很弱智不说, 询问4以前做过类似的,好像是USACO月赛hotel那题类似,维护lmax,rmax,max三个域就可以了 操作0,操作1也很简单,仔细考虑一下就知道也是可以l ...

  4. POJ_1182_食物链_[NOI]_(并查集)

    描述  http://poj.org/problem?id=1182 共A,B,C三种动物,A吃B,B吃C,C吃A.给出询问 q : t , x , y , 表示: x 与 y 是同类 ( t==1 ...

  5. 使用SQL Server 2014内存数据库时需要注意的地方

    作者 王枫发布于2014年7月4日 本文从产品设计和架构角度分享了Microsoft内存数据库方面的使用经验,希望你在阅读本文之后能够了解这些新的对象.概念,从而更好地设计你的架构. 内存数据库,指的 ...

  6. 【转】Android:Animation的简单学习--不错

    原文网址:http://blog.csdn.net/huangbiao86/article/details/6683665 Animation动画效果.提供了一系列的动画效果,可以应用大多数 的控件. ...

  7. DATE_FORMAT()(转)

    mysql中DATE_FORMAT(date, format)函数可根据format字符串格式化日期或日期和时间值date,返回结果串. 也可用DATE_FORMAT( ) 来格式化DATE 或DAT ...

  8. hibernate数据库方言

    hibernate数据库方言 mark一下 RDBMS 方言 DB2 org.hibernate.dialect.DB2Dialect DB2 AS/400 org.hibernate.dialect ...

  9. Web Service学习笔记

    Web Service概述 Web Service的定义 W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API ...

  10. (已解决 7.8号)leecode 分词利用词典分词 word break

    不戚戚于贫贱,不汲汲于富贵      ---五柳先生 Given a string s and a dictionary of words dict, determine if s can be se ...