本文翻译自:How to pass parameters using ui-sref in ui-router to controller

I need to pass and recieve two parameters to the state I want to transit to using ui-sref of ui-router. 我需要使用ui-sref的ui- ui-sref将两个参数传递给我想转移到的状态。

Something like using the link below for transitioning the state to home with foo and bar parameters: 类似于使用以下链接将状态转换为带有foobar参数的home

<a ui-sref="home({foo: 'fooVal', bar: 'barVal'})">Go to home state with foo and bar parameters </a>

Receiving foo and bar values in a controller: 在控制器中接收foobar值:

app.controller('SomeController', function($scope, $stateParam) {//..var foo = $stateParam.foo; //getting fooValvar bar = $stateParam.bar; //getting barVal//..
});

I get undefined for $stateParam in the controller. 我在控制器中为$stateParam undefined的内容。

Could somebody help me understand how to get it done? 有人可以帮我理解如何完成它吗?

Edit: 编辑:

.state('home', {url: '/',views: {'': {templateUrl: 'home.html',controller: 'MainRootCtrl'},'A@home': {templateUrl: 'a.html',controller: 'MainCtrl'},'B@home': {templateUrl: 'b.html',controller: 'SomeController'}}});

#1楼

参考:https://stackoom.com/question/1jc4c/如何使用ui-router中的ui-sref将参数传递给控制器


#2楼

I've created an example to show how to. 我已经创建了一个示例来展示如何。 Updated state definition would be: 更新的state定义将是:

  $stateProvider.state('home', {url: '/:foo?bar',views: {'': {templateUrl: 'tpl.home.html',controller: 'MainRootCtrl'},...}

And this would be the controller: 这将是控制器:

.controller('MainRootCtrl', function($scope, $state, $stateParams) {//..var foo = $stateParams.foo; //getting fooValvar bar = $stateParams.bar; //getting barVal//..$scope.state = $state.current$scope.params = $stateParams;
})

What we can see is that the state home now has url defined as: 我们可以看到,州家现在已将网址定义为:

url: '/:foo?bar',

which means, that the params in url are expected as 这意味着,网址中的参数预计为

/fooVal?bar=barValue

These two links will correctly pass arguments into the controller: 这两个链接将正确地将参数传递到控制器:

<a ui-sref="home({foo: 'fooVal1', bar: 'barVal1'})">
<a ui-sref="home({foo: 'fooVal2', bar: 'barVal2'})">

Also, the controller does consume $stateParams instead of $stateParam . 此外,控制器确实使用$stateParams而不是$stateParam

Link to doc: 链接到doc:

  • URL Parameters 网址参数

You can check it here 你可以在这里查看

params : {}

There is also new , more granular setting params : {} . 还有新的 ,更精细的设置params : {} As we've already seen, we can declare parameters as part of url . 正如我们已经看到的,我们可以将参数声明为url一部分。 But with params : {} configuration - we can extend this definition or even introduce paramters which are not part of the url: 但是使用params : {}配置 - 我们可以扩展这个定义,甚至引入不属于url的参数:

.state('other', {url: '/other/:foo?bar',params: { // here we define default value for foo// we also set squash to false, to force injecting// even the default value into urlfoo: {value: 'defaultValue',squash: false,},// this parameter is now array// we can pass more items, and expect them as []bar : { array : true,},// this param is not part of url// it could be passed with $state.go or ui-sref hiddenParam: 'YES',},...

Settings available for params are described in the documentation of the $stateProvider 可用于params的设置在$ stateProvider的文档中描述

Below is just an extract 以下只是摘录

  • value - {object|function=} : specifies the default value for this parameter. value - {object | function =} :指定此参数的默认值。 This implicitly sets this parameter as optional... 这隐式地将此参数设置为可选...
  • array - {boolean=}: (default: false) If true, the param value will be treated as an array of values. array - {boolean =} :( default:false)如果为true,则param值将被视为值数组。
  • squash - {bool|string=}: squash configures how a default parameter value is represented in the URL when the current parameter value is the same as the default value. squash - {bool | string =}: squash配置当前参数值与默认值相同时,URL中如何表示默认参数值。

We can call these params this way: 我们可以这样称呼这些参数:

// hidden param cannot be passed via url
<a href="#/other/fooVal?bar=1&amp;bar=2">
// default foo is skipped
<a ui-sref="other({bar: [4,5]})">

Check it in action here 在这里检查它


#3楼

You don't necessarily need to have the parameters inside the URL. 您不一定需要在URL中包含参数。

For instance, with: 例如,用:

$stateProvider
.state('home', {url: '/',views: {'': {templateUrl: 'home.html',controller: 'MainRootCtrl'},},params: {foo: null,bar: null}
})

You will be able to send parameters to the state, using either: 您可以使用以下任一方法将参数发送到州:

$state.go('home', {foo: true, bar: 1});
// or
<a ui-sref="home({foo: true, bar: 1})">Go!</a>

Of course, if you reload the page once on the home state, you will loose the state parameters, as they are not stored anywhere. 当然,如果您在home状态下重新加载一次页面,则会丢失状态参数,因为它们不会存储在任何位置。

A full description of this behavior is documented here , under the params row in the state(name, stateConfig) section. 此处记录了此行为的完整描述,位于state(name, stateConfig)部分的params行下。


#4楼

You simply misspelled $stateParam , it should be $stateParams (with an s). 你只是拼错了$stateParam ,它应该是$stateParams (带有s)。 That's why you get undefined ;) 这就是你未定义的原因;)

如何使用ui-router中的ui-sref将参数传递给控制器相关推荐

  1. html mint ui,vue中Mint UI是什么?

    在vue中,Mint UI是饿了么团队开源的一款基于Vue.js的移动端组件库.Mint UI包含丰富的CSS和JS组件,能够满足日常的移动端开发需要:通过它,可以快速构建出风格统一的页面,提升开发效 ...

  2. 为什么不能在子线程中更新UI

    首先声明一点:子线程里面是可以更新UI的--创建一个空白的Activity,在其xml文件中放一个空白TextView,Java代码如下: @Override protected void onCre ...

  3. 5.UI线程和非UI线程的交互方式

    转载请标明出处:  http://blog.csdn.net/yujun411522/article/details/46041637 本文出自:[yujun411522的博客] 这里说的交互方式应该 ...

  4. 在UI设计中如何正确使用颜色

    在我们进行UI设计时,颜色往往是我们表达理念重要的一点,但也是我们常常会忽视的一点.颜色其实如同语言一样,我们可以通过颜色的变化组合,来表达不同的情绪. 正确使用颜色不单能让我们吸引到客户,也能更好地 ...

  5. UI自动化测试中的页面定位问题,年薪50W软件测试工程师为你解答

    这几天有人问我,UI自动化测试中使用到的页面定位元素应该存放在哪里比较合适? 我想说的是,如果你使用的是PO设计模式设计测试用例的话,可以把定位元素存在每一个page页面或者单独存放在一个目录中,新键 ...

  6. HTML5 Web app开发工具Kendo UI Web中图像浏览器的使用

    2019独角兽企业重金招聘Python工程师标准>>> Kendo UI Web中的图像浏览器在默认的情况下会打开一个简单的对话框,如下图所示,方便用户键入或者是粘贴图片的URL以及 ...

  7. 【Based Android】Android Sensor感应器介绍(二)线程中刷新UI 创建一个android测力计...

    上一篇文章http://www.cnblogs.com/octobershiner/archive/2011/11/06/2237880.html介绍了sensor的基本知识以及一个使用其中加速度感应 ...

  8. 二十四、Struts2中的UI标签

    二十四.Struts2中的UI标签 Struts2中UI标签的优势: 数据回显 页面布局和排版(Freemark),struts2提供了一些常用的排版(主题:xhtml默认 simple ajax) ...

  9. Android利用Looper在子线程中改变UI

    MainActivity如下: package cn.testlooper; import android.app.Activity; import android.os.Bundle; import ...

  10. ActionScript工程如何使用Flash CS的fl包中的UI组件(转)

    最近在看ActionScript 3.0 设计模式,书中的例子都是在Flash CS3中开发和测试通过的.我用的开发环境是Flex 3,我新建了一个ActionScript 项目,需要使用fl组件包. ...

最新文章

  1. 投资房地产,甘当'接盘侠'才能赚到钱?
  2. 重温强化学习之无模型学习方法:蒙特卡洛方法
  3. 解决Fedora没有最大化最小化按钮
  4. 【Python】Python办公自动化 | 一键给PDF文件加密,超方便
  5. Wireshark的https代理抓包(whistle中间人代理)
  6. (四)boost库之正则表达式regex
  7. QT4.8界面设计(MSVC2010X)+位姿哈希+ICP结果
  8. 数据绑定和第一个AngularJS 应用
  9. git常用操作,切换分支,合并分支
  10. prometheus修改数据保留时间
  11. iPhone 13或有8款配色;vivo百万年薪招工程师;特斯拉新增行车记录视频紧急情况自动保存功能|极客头条...
  12. 显示出eclipse文件层次
  13. HDU 5936 2016CCPC杭州 D: Difference(折半枚举)
  14. 雷林鹏分享:jQuery EasyUI 树形菜单 - 创建带复选框的树形菜单
  15. [js高手之路]Node.js模板引擎教程-jade速学与实战1-基本用法
  16. 51物联卡:物联卡支持5G吗?怎么购买5G物联卡
  17. 小程序瀑布流-是真的流啊
  18. 央视家庭厨房节目 <天天饮食> 43道家常菜
  19. AE圣诞树(html版本),免费
  20. pydicom 安装与使用

热门文章

  1. 蓝牙协议 HFP,HSP,A2DP等等
  2. ScrollView和ListView冲突解决
  3. Android事件机制:事件传递和消费
  4. 【Gradle】管理库工程release及debug
  5. linux编译树莓派内核,编译树莓派 4B Linux 5.9 内核
  6. python程序员面试宝典 勘误_《前端面试江湖》勘误合集(二)
  7. 2018-2019-2 网络对抗技术 20165337 Exp4 恶意代码分析
  8. 开发过程中快速抓包并解析
  9. MySQL如何修改密码
  10. JDBC oracle 错误总结