基本参数:

‘/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

最新文章

  1. console的花式用法
  2. Android Fragment 使用技巧
  3. 继续说一下openjson 以及 json path 的使用 (2)
  4. 【转载】给VM虚拟机增加硬盘容量
  5. hihoCoder 1195 高斯消元.一
  6. hdu 4289 最大流拆点
  7. xenserver
  8. QT中QWidget、QDialog QMainWindow
  9. c#自定义控件属性面板及选择资源设置
  10. [置顶] [混迹IT职场系列]一、转正的那些事儿
  11. 机器学习笔记5-Tensorflow高级API之tf.estimator
  12. [Ext.Net]TreePanel+gridPanel实例
  13. poj100纪念
  14. redis安装,windows,linux版本并部署服务
  15. Spring Boot 构建电商基础秒杀项目 (九) 商品列表 &amp; 详情
  16. [BUAA软工]第一次结对作业
  17. LintCode: Identical Binary Tree
  18. noi 2727:仙岛求药
  19. 黄聪:VPS服务器如何配置PHP.ini解决wordpress使用WP-Mail-SMTP插件发邮件出现Could not connect to SMTP host的解决办法
  20. 浅谈DB2的四个隔离级别

热门文章

  1. 手动清除或刷新Linux的Swap分区
  2. Composer安装和laravel下载
  3. Linux进程(作业)的查看和杀死 牛
  4. MathType中带上下标字符不对其
  5. java接入极光推送
  6. VMware8安装MacOS 10.8
  7. TensorFlow------TFRecords的读取实例
  8. Add Two Numbers(from leetcode python 链表)
  9. Axure 简单原型设计
  10. scrapy-splash抓取动态数据例子六