基本参数:

‘/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. C# Sftp操作

    SFTP释义-----引自百度百科 sftp是Secure File Transfer Protocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的网络的加密方法.sftp 与 ftp 有着 ...

  2. Android内存优化12 内存泄漏常见情况3 注册泄漏

    android 中有很多注册和反注册,由于在注册后,上下文自身会被持久化的观察者列表所持有,如果不进行反注册,就会造成内存泄漏 内存泄漏1:Sensor Manager 代码如下: MainActiv ...

  3. easyui datagrid 动态生成列

    for (var item_key in data) {//遍历json对象的每个key/value对,p为key var reg = /^score\d+/gi; for (var key in d ...

  4. 万里长征第二步——django个人博客(第六步 ——添加富文本编辑器)

    下载kindeditor 在admin.py文件中配置链接 class Media: js = ( '/static/js/kindeditor-4.1.10/kindeditor-min.js', ...

  5. 深入C(关键字)

    C语言标准定义的32个关键字 关键字 意 义 auto 声明自动变量,缺省时编译器一般默认为auto int 声明整型变量 double 声明双精度变量 long 声明长整型变量 char 声明字符型 ...

  6. sudoers文件属性的修改导致sudo命令失效

    1.sudoers文件属性为440 -r--r-----. 1 root root 4033 Dec 28 18:57 sudoers 2.如果修改sudoers文件属性,就会导致sudo命令不可用, ...

  7. [转]ssis cannot retrieve the column code page info from the ole db provider

    本文转自:http://social.msdn.microsoft.com/Forums/sqlserver/en-US/dc1a61f2-1ab8-4ed3-b85c-db6481800b50/er ...

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

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

  9. python查看字节码

    查看字节码可以帮助我们更好的理解python的执行流程 查看字节码列表 import opcode for op in range(len(opcode.opname)): print('0x%.2X ...

  10. Linux now!--网络配置

    第一种:使用命令修改(直接即时生效,重启失效) #ifconfig eth0 192.168.0.1 netmask 255.255.255.0 up 说明: eth0是第一个网卡,其他依次为eth1 ...