UI-Router -更改$state,而不需要重新加载页面。

时间:2021-10-26 20:59:57

I've been looking at these pages (1, 2, 3). I basically want to change my $state, but I don't want the page to reload.

我一直在看这些页面(1,2,3),我基本上想要改变$状态,但我不希望页面重载。

I am currently in the page /schedules/2/4/2014, and I want to go into edit mode when I click a button and have the URL become /schedules/2/4/2014/edit.

我现在在页面/进度表/2/4/2014,我想在点击按钮时进入编辑模式,URL变为/schedule /2/4/2014/edit。

My edit state is simply $scope.isEdit = true, so there is no point of reloading the whole page. However, I do want the $state and/or url to change so that if the user refreshses the page, it starts in the edit mode.

我的编辑状态只是$scope。isEdit = true,所以没有必要重新加载整个页面。但是,我确实希望$state和/或url更改,以便如果用户重新刷新页面,它将在编辑模式中启动。

What can I do?

我能做什么?

2 个解决方案

#1


19  

For this problem, you can just create a child state that has neither templateUrl nor controller, and advance between states normally:

对于这个问题,您可以创建一个既没有templateUrl也没有控制器的子状态,并在状态之间正常前进:

// UPDATED
$stateProvider
    .state('schedules', {
        url: "/schedules/:day/:month/:year",
        templateUrl: 'schedules.html',
        abstract: true, // make this abstract
        controller: function($scope, $state, $stateParams) {
            $scope.schedDate = moment($stateParams.year + '-' + 
                                      $stateParams.month + '-' + 
                                      $stateParams.day);
            $scope.isEdit = false;

            $scope.gotoEdit = function() {
                $scope.isEdit = true;
                $state.go('schedules.edit');
            };

            $scope.gotoView = function() {
                $scope.isEdit = false;
                $state.go('schedules.view');
            };
        },
        resolve: {...}
    })
    .state('schedules.view', { // added view mode
        url: "/view"
    })
    .state('schedules.edit', { // both children share controller above
        url: "/edit"
    });

An important concept here is that, in ui-router, when the application is in a particular state—when a state is "active"—all of its ancestor states are implicitly active as well.

这里的一个重要概念是,在ui-router中,当应用程序处于特定状态时——当一个状态是“活动的”时——它的所有祖先状态也都是隐式活动的。

So, in this case,

所以,在这种情况下,

  • when your application advances from view mode to edit mode, its parent state schedules (along with its templateUrl, controller and even resolve) will still be retained.
  • 当您的应用程序从视图模式前进到编辑模式时,它的父状态计划(连同它的templateUrl、控制器甚至解析)仍将保留。
  • since ancestor states are implicitly activated, even if the child state is being refreshed (or loaded directly from a bookmark), the page will still render correctly.
  • 由于祖先状态被隐式激活,即使子状态被刷新(或直接从书签中加载),页面仍然会正确呈现。

#2


20  

REF: https://github.com/angular-ui/ui-router/wiki/Quick-Reference#statetransitiontoto-toparams--options

裁判:https://github.com/angular-ui/ui-router/wiki/Quick-Reference statetransitiontoto-toparams——选项

$state.transitionTo('yourState', params, {notify: false});

#1


19  

For this problem, you can just create a child state that has neither templateUrl nor controller, and advance between states normally:

对于这个问题,您可以创建一个既没有templateUrl也没有控制器的子状态,并在状态之间正常前进:

// UPDATED
$stateProvider
    .state('schedules', {
        url: "/schedules/:day/:month/:year",
        templateUrl: 'schedules.html',
        abstract: true, // make this abstract
        controller: function($scope, $state, $stateParams) {
            $scope.schedDate = moment($stateParams.year + '-' + 
                                      $stateParams.month + '-' + 
                                      $stateParams.day);
            $scope.isEdit = false;

            $scope.gotoEdit = function() {
                $scope.isEdit = true;
                $state.go('schedules.edit');
            };

            $scope.gotoView = function() {
                $scope.isEdit = false;
                $state.go('schedules.view');
            };
        },
        resolve: {...}
    })
    .state('schedules.view', { // added view mode
        url: "/view"
    })
    .state('schedules.edit', { // both children share controller above
        url: "/edit"
    });

An important concept here is that, in ui-router, when the application is in a particular state—when a state is "active"—all of its ancestor states are implicitly active as well.

这里的一个重要概念是,在ui-router中,当应用程序处于特定状态时——当一个状态是“活动的”时——它的所有祖先状态也都是隐式活动的。

So, in this case,

所以,在这种情况下,

  • when your application advances from view mode to edit mode, its parent state schedules (along with its templateUrl, controller and even resolve) will still be retained.
  • 当您的应用程序从视图模式前进到编辑模式时,它的父状态计划(连同它的templateUrl、控制器甚至解析)仍将保留。
  • since ancestor states are implicitly activated, even if the child state is being refreshed (or loaded directly from a bookmark), the page will still render correctly.
  • 由于祖先状态被隐式激活,即使子状态被刷新(或直接从书签中加载),页面仍然会正确呈现。

#2


20  

REF: https://github.com/angular-ui/ui-router/wiki/Quick-Reference#statetransitiontoto-toparams--options

裁判:https://github.com/angular-ui/ui-router/wiki/Quick-Reference statetransitiontoto-toparams——选项

$state.transitionTo('yourState', params, {notify: false});