如何在不使用rootScope或URL参数的情况下在AngularJS中的两个状态之间发送对象?

时间:2022-08-24 12:51:30

When I navigate between one state to another, I want to pass an object to be used in the new state. How can I do this without using global object solutions such as $rootScope? Also I don't want to use the state url parameters.

当我在一个状态到另一个状态之间导航时,我想传递一个要在新状态下使用的对象。如何在不使用全局对象解决方案(如$ rootScope)的情况下执行此操作?另外我不想使用状态url参数。

Basically, I want to do $state.go('myState', {some: 'thing'}) and inside the controller for "myState" I want to be able to reach the passed object somehow.

基本上,我想做$ state.go('myState',{some:'thing'})并在控制器内部为“myState”我希望能够以某种方式到达传递的对象。

3 个解决方案

#1


1  

I assume that by "two states" you mean two different controllers, each of them accessible through different routes.

我假设“两个状态”是指两个不同的控制器,每个控制器可以通过不同的路径访问。

Angular would recommend to use a service for that as the doc says :

Angular建议使用服务,因为文档说:

Angular services are substitutable objects that are wired together using dependency injection (DI). You can use services to organize and share code across your app.

角度服务是使用依赖注入(DI)连接在一起的可替换对象。您可以使用服务在整个应用中组织和共享代码。

#2


0  

What do you mean between states? If you mean between controllers, then you could use a shared service to hold the data. Services are singletons, so live for the lifetime of the page.

国家之间是什么意思?如果您的意思是控制器之间,那么您可以使用共享服务来保存数据。服务是单身,所以在页面的生命周期中都是如此。

#3


0  

you can access state parameters in your

你可以访问你的状态参数

.controller('ctrl',['$scope','$stateParams',function($scope , $stateParams ) {    

          $scope.getParams = function() {
              console.log( $stateParams.some ); 
          }

    }

https://github.com/angular-ui/ui-router/wiki/URL-Routing#stateparams-service

#1


1  

I assume that by "two states" you mean two different controllers, each of them accessible through different routes.

我假设“两个状态”是指两个不同的控制器,每个控制器可以通过不同的路径访问。

Angular would recommend to use a service for that as the doc says :

Angular建议使用服务,因为文档说:

Angular services are substitutable objects that are wired together using dependency injection (DI). You can use services to organize and share code across your app.

角度服务是使用依赖注入(DI)连接在一起的可替换对象。您可以使用服务在整个应用中组织和共享代码。

#2


0  

What do you mean between states? If you mean between controllers, then you could use a shared service to hold the data. Services are singletons, so live for the lifetime of the page.

国家之间是什么意思?如果您的意思是控制器之间,那么您可以使用共享服务来保存数据。服务是单身,所以在页面的生命周期中都是如此。

#3


0  

you can access state parameters in your

你可以访问你的状态参数

.controller('ctrl',['$scope','$stateParams',function($scope , $stateParams ) {    

          $scope.getParams = function() {
              console.log( $stateParams.some ); 
          }

    }

https://github.com/angular-ui/ui-router/wiki/URL-Routing#stateparams-service