`UI-router` $ stateParams与$ state.params [英] `ui-router` $stateParams vs. $state.params

查看:163
本文介绍了`UI-router` $ stateParams与$ state.params的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 UI路由器,有可能注入或者 $状态 $ stateParams 到控制器以访问参数中的URL。然而,通过 $ stateParams 访问参数仅公开属于由访问它的控制器管理的状态参数,及其母公司状态,而 $状态。 PARAMS 的所有参数,包括那些在任何一个孩子的状态。

With ui-router, it's possible to inject either $state or $stateParams into a controller to get access to parameters in the URL. However, accessing parameters through $stateParams only exposes parameters belonging to the state managed by the controller that accesses it, and its parent states, while $state.params has all parameters, including those in any child states.

的http://路径/ 1 / paramA上/ paramB ,这是怎么一回事呢控制器时负载:

Given the following code, if we directly load the URL http://path/1/paramA/paramB, this is how it goes when the controllers load:

$stateProvider.state('a', {
     url: 'path/:id/:anotherParam/',
     controller: 'ACtrl',
  });

$stateProvider.state('a.b', {
     url: '/:yetAnotherParam',
     controller: 'ABCtrl',
  });

module.controller('ACtrl', function($stateParams, $state) {
   $state.params; // has id, anotherParam, and yetAnotherParam
   $stateParams;  // has id and anotherParam
}

module.controller('ABCtrl', function($stateParams, $state) {
   $state.params; // has id, anotherParam, and yetAnotherParam
   $stateParams;  // has id, anotherParam, and yetAnotherParam
}

现在的问题是,为什么不同?以及是否有最佳实践准则围绕何时以及为什么要使用或避免使用它们中的?

The question is, why the difference? And are there best practices guidelines around when and why you should use, or avoid using either of them?

推荐答案

该文档在这里重申你的发现:<一href=\"https://github.com/angular-ui/ui-router/wiki/URL-Routing#stateparams-service\">https://github.com/angular-ui/ui-router/wiki/URL-Routing#stateparams-service

The documentation reiterates your findings here: https://github.com/angular-ui/ui-router/wiki/URL-Routing#stateparams-service

如果我没有记错, $ stateParams 后来推出比原 $ state.params ,似是是一个简单的辅助喷射器,以避免不断地写 $ state.params

If my memory serves, $stateParams was introduced later than the original $state.params, and seems to be a simple helper injector to avoid continuously writing $state.params.

我怀疑有任何的最佳做法指南,但胜方面为我。如果你只是想访问PARAMS接收到的URL,然后用 $ stateParams 。如果你想了解一下国家本身比较复杂,使用 $状态

I doubt there are any best practice guidelines, but context wins out for me. If you simply want access to the params received into the url, then use $stateParams. If you want to know something more complex about the state itself, use $state.

这篇关于`UI-router` $ stateParams与$ state.params的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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