如果只有params改变,停止角度ui-router重新加载状态的模板和控制器?

时间:2022-11-21 19:41:08

I have state defined like:

我有州定义如下:

.state("root.home.foo", {
   url: "/foo/:id",
   templateUrl: "/static/partials/home.foo.html",
   controller: 'FooCtrl'
})

When in that state, I respond to a mouse click, wanting to change the parameter...

当处于该状态时,我响应鼠标单击,想要更改参数...

$state.go('root.home.foo', { id: newId });

But I'm getting flicker as it reloads the state's template and re-create's the controller. Can I make it so that the controller is not recreated? It can respond to a $stateChangeSuccess event, and that's all I need.

但是当我重新加载状态模板并重新创建控制器时,我会闪烁。我可以这样做,以便不重新创建控制器吗?它可以响应$ stateChangeSuccess事件,这就是我所需要的。

Update

更新

Following Jason's suggestion, I tried a sub-state. It works.

根据杰森的建议,我尝试了一个子州。有用。

.state("root.home.foo", {
   abstract: true,
   url: "/foo",
   templateUrl: "/static/partials/home.foo.html",
   controller: 'FooCtrl'
})
.state("root.home.foo.itemSelected", {        
   url: "/:foo"
})

Listening to the $stateChangeSuccess event...

听$ stateChangeSuccess事件......

// In controller's constructor
$scope.$on('$stateChangeSuccess', function (e, to, toParams, from, fromParams) {
  if ($state.current.name == 'root.home.foo.itemSelected') {       
    handleSelection(toParams.id);
  }
});

1 个解决方案

#1


1  

Try setting reloadOnSearch: false in the route definition. E.g:

尝试在路由定义中设置reloadOnSearch:false。例如:

.state("root.home.foo", {
  url: "/foo/:id",
  templateUrl: "/static/partials/home.foo.html",
  controller: 'FooCtrl',
  reloadOnSearch: false
})

#1


1  

Try setting reloadOnSearch: false in the route definition. E.g:

尝试在路由定义中设置reloadOnSearch:false。例如:

.state("root.home.foo", {
  url: "/foo/:id",
  templateUrl: "/static/partials/home.foo.html",
  controller: 'FooCtrl',
  reloadOnSearch: false
})