AngularJS ui-router: reload:true also reloads parent state

喜你入骨 提交于 2019-12-19 16:52:07

问题


In this plunk you have two ui-router states, a parent and a child. When the child is invoked by clicking on the link, since it has the option reload: true it is always reloaded. This is fine, but the problem is that the parent state is reloaded as well. Try to click on the 'Populate 11' link several times and you'll see that the parent timestamp also changes.

How can I reload only the child?

Javascript:

var app = angular.module("app", ['ui.router']);

app.config(function($stateProvider, $urlRouterProvider) {

  $urlRouterProvider.otherwise("/");

  $stateProvider
    .state('state1', {
          templateUrl: 'state1.html',
          controller: function($scope) {

            $scope.theTime1 = Date.now();

          }
    })
    .state('state1.state11', {
          templateUrl: 'state11.html',
          controller: function($scope) {

               $scope.theTime11 = Date.now();

         }
    });

});

回答1:


It's actually very simple.

Don't use reload because that does exactly what you found. It reloads everything for the route.

Instead, add a parameter to your child route and every time the link is clicked make sure to change that parameter. That will force the child state to be reloaded.

I updated your plunk with an example. I just added a num parameter and increase a count variable each time the link is clicked.

http://plnkr.co/edit/qTA39rrYFYUegzuFbWnc?p=preview




回答2:


A changing parameter as Mathew Foscarini suggested, is (for sure) way to go. There could be also another solution, technique with state reloader. Below, and in this updated plunker we can see simplified version, but we can even pass some params there to make it more general

.state('state1.reloader', {
      controller: function($state) {
        $state.go('state1.state11')
      }
})

And we can call it like:

// instead of this
<a href="#" ui-sref="state1.state11" ui-sref-opts="{reload: true}">
// we can do this
<a href="#" ui-sref="state1.reloader" >

Check it here



来源:https://stackoverflow.com/questions/27662818/angularjs-ui-router-reloadtrue-also-reloads-parent-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!