本文介绍了如何在不重新加载 ui-view 的情况下更新 $stateParams?角度ui路由器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取上下文,角度,ui-router,没什么特别的,一个用 3 个名为 ui-views 构建的根视图.所以在 index.html 我们有

Get the context, angular, ui-router, nothing special, a root view built with 3 named ui-views.so in index.html we have

<body>
  <div ui-view='left'>
  <div ui-view='center'>
  <div ui-view='right'>
</body>

我的路线看起来像

$stateProvider
 .state('main', {
  url: '/',
  views: {
   'left': {templateUrl: 'foo.html'},
   'center': {templateUrl: 'bar.html'},
   'right': {templateUrl: 'xyz.html'}
  }
 })
 .state('main.b', {
  url: '/b',
  params: { foo: {value: 'bar'} }
  views: { 'right@': {templateUrl: '123.html'} } // I wish to update $stateParams in 'left@' view
 })
 .state('main.c', {
  url: '/c',
  params: ...
  views: { 'left@': ..., 'center@': ..., 'right@': .. }
 });

有没有办法去b状态更新'center'和'left'视图中的$stateParams??我可以使用服务获得它,但我需要将 $watch 添加到我需要的变量中,对我来说它看起来有点笨拙.

Is there a way in going to b state to update the $stateParams in the 'center' and 'left' view?? I can get it using a service but i need to add a $watch to the variable I need and it looks a little bit hacky to me.

进入 c 状态我实际上可以得到我想要的东西,但是视图被重新加载,我希望避免这种行为,因为我在左"视图中有一个画布.

Going into c state I can actually get what I want, but the view is reloaded, and i wish to avoid this behaviour cause i have a canvas in the 'left' view.

推荐答案

您可以使用以下内容转到特定路线而无需重新加载视图:

You could use the following to go to a specific route without reloading the views:

$state.go('.', {parm1: 1}, {notify: false});

最后一个对象字面量表示您可以传递给 go 的选项.如果您将 notify 设置为 false,这实际上会阻止控制器重新初始化.开头的 . 是你要去的绝对状态名或相对状态路径.

The last object literal represents the options which you can pass along to go. If you set notify to false, this will actually prevent the controllers from being reinitialized. The . at the beginning is the absolute state name or relative state path you wanna go to.

重要的是notify.

这篇关于如何在不重新加载 ui-view 的情况下更新 $stateParams?角度ui路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:12