简体   繁体   English

Angular:UI路由器如何通过状态将数据对象或某些参数传递给其他控制器

[英]Angular: ui-router how to pass data object or some parameter to other controller through state

I'm already read about resolve, data for ui-router but still can not get how I can solve my problem? 我已经读过有关ui-router的解决方案和数据的信息,但仍然无法获得解决问题的方法?

View 1 HTML: 查看1个HTML:

<a ui-sref="configuration.settings.list"><span>Settings</span></a>
<a ui-sref="configuration.settings.personal({user: userId})"><span >Personal</span></a>

userId is variable which is defined in the controller for current view. userId是在控制器中为当前视图定义的变量。

Route definition: 路线定义:

angular.module("app").config(["$stateProvider", "$urlRouterProvider", "$locationProvider", 
function($stateProvider, $urlRouterProvider, $locationProvider) {


    $stateProvider

    .state('configuration.settings.lis', {
        url: '/',
        templateUrl: 'html/settings',
        controller: 'settingsController',
    }),
    .state('configuration.settings.personal', {
        url: '/:userId',
        templateUrl: 'html/settings',
        controller: 'personalController',
    }),

I can access userId in personalController through stateParams . 我可以通过stateParams访问personalController userId

Question: I want pass object or some parameter from View 1 to personalController without `stateParams. 问题:我想将对象或某些参数从View 1传递到personalController而不使用`stateParams。 Is it possible? 可能吗?

Something like that: 像这样:

<a ui-sref="configuration.settings.personal({myDamnCooLObject})"><span >Personal</span></a>


.state('configuration.settings.personal', {
        url: '/', <-- NO attributes here!!!
        templateUrl: 'html/settings',
        controller: 'personalController',
        data: {{myDamnCooLObject}} <-- THIS object or parameter shuld be accesseble from 'personalController'
    })

$stateParams are only guaranteed to be preserved for target routes that use them. 仅保证为使用$stateParams的目标路由保留$stateParams ui-router actually keeps this object clean so you can't just stuff random things into it. ui-router实际上使该对象保持干净,因此您不能只是将随机的东西塞进其中。

I've had good luck using a service as a manager object to persist items between views. 我很幸运使用服务作为管理器对象来在视图之间保留项目。 For example, I have an app that has a user profile page and a photo gallery as a separate view. 例如,我有一个应用程序,其中包含用户个人资料页面和照片库作为单独的视图。 I want a lot of the user's core data over in the gallery because there's a title with their name, links back, a little bio box on the side, etc. 我希望在画廊中获得很多用户的核心数据,因为有一个标题,包括他们的名字,链接,侧面有一个小生物框等。

The way I wired this up, both views have a user ID in their URL as a parameter. 我进行连接的方式是,两个视图在其URL中都有一个用户ID作为参数。 I have a manager that maintains a cache of user data, and a getter that returns a promise to return it. 我有一个管理器,该管理器维护用户数据的缓存,而一个getter返回一个将其返回的承诺。 In both states, I have a resolve block that takes the user ID and asks the manager for the whole data object. 在这两种状态下,我都有一个resolve块,该块使用用户ID并向管理器询问整个数据对象。 The manager makes a Web service call to get the data the first time, and immediately resolves for the second call (doesn't matter who gets there first). 经理第一次进行Web服务调用以获取数据,然后立即解决第二次调用(谁先到达那里都无所谓)。 It works very well and it's lightweight. 它运行良好,重量轻。

In your example you don't want a parameter in the URL at all, but the model could work the same way. 在您的示例中,您根本不需要URL中的参数,但是模型可以以相同的方式工作。 Services are global singletons so you can use them as a dumping ground for all kinds of things to be passed around and persisted through things like state transitions. 服务是全局单例,因此您可以将它们用作各种事物传递和通过状态转换之类的持久化的垃圾场。 Obviously this is a slippery slope because there are many bad programming practices that are also enabled once you start doing this - but with care and good patterns, it's a very useful technique. 显然,这是一个滑坡,因为一旦开始执行此操作,也会启用许多不良的编程习惯-但要小心并拥有良好的模式,这是一种非常有用的技术。

Note that a lot of people actually use $rootScope for this same purpose - it has basically the same effect. 请注意,很多人实际上出于相同的目的使用$rootScope它具有基本相同的效果。 But I personally believe a service is better because that way you have a very explicit, defined wrapper around this data you need to pass around, it's much more testable, and it's easier to document it. 但我个人认为服务会更好,因为这样您就可以对需要传递的数据进行非常显式的定义包装,它的可测试性更高,并且记录起来也更加容易。 With $rootScope you're just dumping raw variables into a huge pile and it becomes a pile of spaghetti to pick apart later, especially if you accidentally re-use a variable name. 使用$rootScope您只是将原始变量转储到一大堆中,而这变成了一堆意大利面条,以便以后拆分,尤其是如果您不小心重用了变量名。 That's causes all kinds of hard-to-find bugs. 这会导致各种难以发现的错误。 With a service you can go so far as to use getters/setters, and you could even wire the state transition ( $state.go() ) call into the setter. 借助服务,您甚至可以使用getter / setter,甚至可以将状态转换( $state.go() )调用连接到setter中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM