基本参数:

‘/user/:id'

'/user/{id}'

'/user/{id:int}'

使用正则表达式:

'/user/{id:[0-9]{1,8}'

'/user/{id:.*}'

'/user/*id

匹配所有以user开始的url 并将剩余参数传给id

多个参数:

‘/user?id1&id2'

$StateParams service

// If you had a url on your state of:
url: '/users/:id/details/{type}/{repeat:[0-9]+}?from&to' // Then you navigated your browser to:
'/users/123/details//0' // Your $stateParams object would be
{ id:'123', type:'', repeat:'0' } // Then you navigated your browser to:
'/users/123/details/default/0?from=there&to=here' // Your $stateParams object would be
{ id:'123', type:'default', repeat:'0', from:'there', to:'here' }

$StateParams仅包含注册在当前状态下的参数,不包含其他状态下的参数,即使是上级的url参数也获取不到

$stateProvider.state('contacts.detail', {
url: '/contacts/:contactId',
controller: function($stateParams){
$stateParams.contactId //*** Exists! ***//
}
}).state('contacts.detail.subitem', {
url: '/item/:itemId',
controller: function($stateParams){
$stateParams.contactId //*** Watch Out! DOESN'T EXIST!! ***//
$stateParams.itemId //*** Exists! ***//
}
})

若想让下级获取到当前状态的参数,需使用resolve()。该函数会在画面渲染出来前先执行完成。

$stateProvider.state('contacts.detail', {
url: '/contacts/:contactId',
controller: function($stateParams){
$stateParams.contactId //*** Exists! ***//
},
resolve:{
contactId: ['$stateParams', function($stateParams){
return $stateParams.contactId;
}]
}
}).state('contacts.detail.subitem', {
url: '/item/:itemId',
controller: function($stateParams, contactId){
contactId //*** Exists! ***//
$stateParams.itemId //*** Exists! ***//
}
})

1 ui-sref、$state.go 的区别

ui-sref 一般使用在 <a>...</a>;

<a ui-sref="message-list">消息中心</a>

$state.go('someState')一般使用在 controller里面;

.controller('firstCtrl', function($scope, $state) {
$state.go('login');
});

这两个本质上是一样的东西,我们看ui-sref的源码:

...
element.bind("click", function(e) {
var button = e.which || e.button;
if ( !(button > 1 || e.ctrlKey || e.metaKey || e.shiftKey || element.attr('target')) ) { var transition = $timeout(function() {
// HERE we call $state.go inside of ui-sref
$state.go(ref.state, params, options);
});
 

ui-sref最后调用的还是$state.go()方法

2 如何传递参数

首先,要在目标页面定义接受的参数:

传参,

ui-sref:

$state.go:

接收参数,

在目标页面的controller里注入$stateParams,然后 "$stateParams.参数名" 获取

 
分类: javascript

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. WPF Interaction框架简介(一)——Behavior

    在WPF 4.0中,引入了一个比较实用的库——Interactions,这个库主要是通过附加属性来对UI控件注入一些新的功能,除了内置了一系列比较好用的功能外,还提供了比较良好的扩展接口.本文这里简单 ...

  2. Oracle10g 创建一个DataBase实例

    Oracle10g创建DataBase实例如下:第一步:Oracle - OraDb10g_home1 -> 配置和移植工具 -> 打开Database Configuration Ass ...

  3. 生成SESSIONID

    生成SESSIONID uses SynCommons procedure TForm1.Button1Click(Sender: TObject);var i: Cardinal;begin i : ...

  4. cocos2d-x Cygwin编译 recipe for target `obj/local/armeabi/libcocos2d.so' fail 解决办法

    在编译cocos2d-x的helloworld 或者 tests的时候. 官网上使用ndk4.ndk5,这里是使用 ndkr7b.ndkr8或ndkr8b .操作会简单很多,但是出了些小问题也是很坑人 ...

  5. void fun() const{}; const void fun(){}; 和void const fun(){}; 的区别?

    void fun() const{}; const void fun(){}; 和void const fun(){}; 的区别? const void fun(){};和void const fun ...

  6. iOS:iOS中的几种动画

    本文来自收藏,感谢原创博主. iOS中的动画 摘要 本文主要介绍核iOS中的动画:核心动画Core Animation, UIView动画, Block动画, UIImageView的帧动画. 核心动 ...

  7. RMAN备份与恢复之概念一

    1.  数据库完全备份: 按归档模式分为归档和非归档 归档模式 打开状态,属于非一致性备份 关闭状态,可以分为一致性和非一致性 非归档模式 打开状态,非一致性备份无效 关闭状态,一致性备份,非一致性备 ...

  8. python3将docx转换成pdf,html文件,pdf转doc文件

    直接上代码 # -*- encoding:utf-8 -*- """ author:lgh 简单的doc转pdf,html,pdf转doc脚本 依赖库pdfminer3k ...

  9. (转)Android技术积累:图片异步加载

    当在ListView或GridView中要加载很多图片时,很容易出现滑动时的卡顿现象,以及出现OOM导致FC(Force Close). 会出现卡顿现象主要是因为加载数据慢,要等数据加载完才能显示出来 ...

  10. centos关闭sudo的ldap认证

    在新服务器上部署项目时,运行sudo命令会卡住很久,然后报错 sudo:ldap_start_tls_s(): Can't contact LDAP server 简直不能忍. 一番研究后发现是lda ...