使用时需要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. perl 初始化Hash

    Vsftp:/root/perl/6# cat a5.pl use Data::Dumper; my @arr=qw/a bc d /; my %rec=(); for $field (@arr){ ...

  2. Learning WCF Chapter1 Exposing Multiple Service Endpoints

    So far in this chapter,I have shown you different ways to create services,how to expose a service en ...

  3. SIFT算法:KeyPoint找寻、定位与优化

    SIFT算法:DoG尺度空间生产  SIFT算法:KeyPoint找寻.定位与优化 SIFT算法:确定特征点方向  SIFT算法:特征描述子 目录: 1.找寻 2.定位 3.优化 1 KeyPoint ...

  4. CSS实现Div透明,而显示在上面的文字不透明,但也可看到显示在下面的图片内容

    CSS实现Div透明,而显示在上面的文字不透明,但也可看到显示在下面的图片内容,DiV透明其实挺简单,主要是为background定义opacity属性,一般这个是最大值是1,数值越接近1,则越不透明 ...

  5. 算法 python实现(三) 快速排序

    算法学起来真费劲啊,智商只够捉只鸡的.昨晚没看明白就没电了,过两天要考虑偷电了... 今天看看快速排序,有一个博客写的很好,通俗生动形象,适合我这样的算法大白菜.推荐一下 http://www.cnb ...

  6. JavaScript---网络编程(8)-DHTML技术演示(1)

    DHTML技术使用的基本思路: 1. 用标签封装数据-html范畴 2. 定义样式-css范畴 3. 明确事件源.事件和要处理的节点-dom范畴 4. 明确具体的操作方式,其实就是事件的处理内容(过程 ...

  7. Bzoj1818: [Cqoi2010]内部白点 && Tyvj P2637 内部白点 扫描线,树状数组,离散化

    1818: [Cqoi2010]内部白点 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 704  Solved: 344[Submit][Status] ...

  8. PHP两个字符串比较(人为出错),两字符串类型和数据表面相等,但strcmp()结果不为0

    PHP中,比较两个字符串是否相等用:strcmp(): PHP strcmp() 函数 PHP String 函数 定义和用法 strcmp() 函数比较两个字符串. 该函数返回: 0 - 如果两个字 ...

  9. PyDev+eclipse的编码问题

    1.在代码的开始声明编码为utf-8

  10. springsecurity4+springboot 实现remember-me 发现springsecurity 的BUG

    前言:现在开发中,记住我这个功能是普遍的,用户不可能每次登录都要输入用户名密码.昨天准备用spring security的记住我功能,各种坑啊,吐血 . 先看下具体实现吧. spring securi ...