admin管理员组文章数量:1310290
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.
What I have for this issue is :
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)?
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?
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.
What I have for this issue is :
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)?
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?
Share Improve this question asked Jun 15, 2016 at 20:31 McFiddlyWiddlyMcFiddlyWiddly 58110 silver badges32 bronze badges 1- Possible duplicate of: stackoverflow./questions/28248236/… – dYale Commented Jun 15, 2016 at 20:36
3 Answers
Reset to default 3So, 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)
In the controller of the first state, state1, I can just call the function
$state.go('state2', {id: self.value} );
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.
Note: you should inject $state, and $stateParams into both controllers.
Service still seems to be your best option to share info between two states. If you need any state information in your service you can just inject $state into it:
......
.service('SomeService', [
'$state',
function ( $state ) {
.......
I think you can pass an image id from state 1 to state 2, and get it in state 2 controller:
$stateProvider
.state('state', {
url: "/test/:id",
templateUrl: 'template.html',
controller: function ($stateParams) {
console.log($stateParams.id);
}
})
and the link from the first page ui-sref="state({id:42})"
本文标签: javascriptHow can I pass data between states with angular uirouterStack Overflow
版权声明:本文标题:javascript - How can I pass data between states with angular ui-router? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741821280a2399378.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论