如何在具有角度ui-router的状态之间传递数据? [英] How can I pass data between states with angular ui-router?

查看:95
本文介绍了如何在具有角度ui-router的状态之间传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我原本不打算用ui-router做这个项目(2天前就知道了),所以我对此很新。

I originally did not intend on doing the project with ui-router (just got to know about it 2 days ago), so I'm quite new at this.

我对此问题的看法是:


  1. 图像列表的模板,使用ng-repeat显示,并获取img -src来自控制器中调用的服务。该服务从json文件获取源。

  1. Template for list of images, shown using ng-repeat, and getting img-src from a service called in the controller. The service gets the source from a json file.

图像编辑器的模板。此控制器应获取在图像列表中单击的图像的图像源。

Template for image editor. The controller for this should get the image source of the image that was clicked in the image list.

如何将图像源从一个状态(图像列表)传递到另一个状态(图像编辑器) )?

How do I pass the image source from one state (the image list) to the another state (the image editor)?

当我假设我不使用ui-router及其状态时,我正在使用服务来做到这一点。我如何使用状态执行此操作?

I was using a service to do that when I was assuming that I won't use ui-router and its states. How do I do this with states?

推荐答案

因此,我找到了一种简单有效的方法来传递数据,这有用对于我所有的案件,直到现在。 (包括与原始问题中提到的场景不同的场景)

So, I found a simple and efficient way to pass data, and this has worked for all of my cases till now. (including scenarios different from the one mentioned in the original question)

在第一个状态state1的控制器中,我可以调用函数

In the controller of the first state, state1, I can just call the function

$state.go('state2', {id: self.value} );

在state2中,我可以通过以下代码获取值:

And in state2, I can obtain the value by the following code:

this.state2value = $state.params.id;

所以可以从一个州传递到另一个,并通过状态参数检索它。

So can pass value from one state to another, and retrieve it through state params.

注意:你应该将$ state和$ stateParams注入两个控制器。

Note: you should inject $state, and $stateParams into both controllers.

这篇关于如何在具有角度ui-router的状态之间传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆