重新加載當前狀態 - 刷新數據

本文翻譯自:Reloading current state - refresh data

I'm using Angular UI Router and would like to reload the current state and refresh all data / re-run the controllers for the current state and it's parent. 我正在使用Angular UI路由器,並希望重新加載當前狀態並刷新所有數據/重新運行控制器以獲取當前狀態及其父級。

I have 3 state levels: directory.organisations.details 我有3個州級別: directory.organisations.details

directory.organisations contains a table with a list of organisations. directory.organisations包含一個包含組織列表的表。 Clicking on an item in the table loads directory.organisations.details with $StateParams passing the ID of the item. 單擊表中的項目會加載directory.organisations.details並使用$ StateParams傳遞項目的ID。 So in the details state I load the details for this item, edit them and then save data. 因此,在詳細信息狀態下,我加載此項目的詳細信息,編輯它們,然後保存數據。 All fine so far. 到目前爲止都很好。

Now I need to reload this state and refresh all the data. 現在我需要重新加載此狀態並刷新所有數據。

I have tried: 我試過了:

 $state.transitionTo('directory.organisations');

Which goes to the parent state but doesn't reload the controller, I guess because the path hasn't changed. 哪個進入父狀態,但沒有重新加載控制器,我想因爲路徑沒有改變。 Ideally I just want to stay in the directory.organisations.details state and refresh all data in the parent too. 理想情況下,我只想保留在directory.organisations.details狀態並刷新父級中的所有數據。

I have also tried: 我也嘗試過:

$state.reload()

I have seen this on the API WIKI for $state.reload "(bug with controllers reinstantiating right now, fixing soon)." 我在API WIKI for $ state.reload上看過這個“(控制器現在重新修復的錯誤,很快就會修復)。”

Any help would be appreciated? 任何幫助,將不勝感激?


#1樓

參考:https://stackoom.com/question/1T6yN/重新加載當前狀態-刷新數據


#2樓

This solution works in AngularJS V.1.2.2: 此解決方案適用於AngularJS V.1.2.2:

$state.transitionTo($state.current, $stateParams, {
    reload: true,
    inherit: false,
    notify: true
});

#3樓

That would be the final solution. 那將是最終的解決方案。 (inspired by @Hollan_Risley's post) (靈感來自@ Hollan_Risley的帖子)

'use strict';

angular.module('app')

.config(function($provide) {
    $provide.decorator('$state', function($delegate, $stateParams) {
        $delegate.forceReload = function() {
            return $delegate.go($delegate.current, $stateParams, {
                reload: true,
                inherit: false,
                notify: true
            });
        };
        return $delegate;
    });
});

Now, whenever you need to reload, simply call: 現在,無論何時需要重新加載,只需調用:

$state.forceReload();

#4樓

I found this to be the shortest working way to refresh with ui-router: 我發現這是用ui-router刷新的最短工作方式:

$state.go($state.current, {}, {reload: true}); //second parameter is for $stateParams

Update for newer versions: 更新版本:

$state.reload();

Which is an alias for: 這是別名:

$state.transitionTo($state.current, $stateParams, { 
  reload: true, inherit: false, notify: true
});

Documentation: https://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$state#methods_reload 文檔: https//angular-ui.github.io/ui-router/site/#/api/ui.router.state.$state#methods_reload


#5樓

For angular v1.2.26, none of the above works. 對於角度v1.2.26,以上都不起作用。 An ng-click that calls the above methods will have to be clicked twice in order to make the state reload. 必須單擊兩次調用上述方法的ng-click才能重新加載狀態。

So I ended up emulating 2 clicks using $timeout. 所以我最終使用$ timeout模擬了2次點擊。

$provide.decorator('$state',
        ["$delegate", "$stateParams", '$timeout', function ($delegate, $stateParams, $timeout) {
            $delegate.forceReload = function () {
                var reload = function () {
                    $delegate.transitionTo($delegate.current, angular.copy($stateParams), {
                        reload: true,
                        inherit: true,
                        notify: true
                    })
                };
                reload();
                $timeout(reload, 100);
            };
            return $delegate;
        }]);

#6樓

for ionic framework 用於離子骨架

$state.transitionTo($state.current, $state.$current.params, { reload: true, inherit: true, notify: true });//reload

$stateProvider.
  state('home', {
    url: '/',
    cache: false, //required

https://github.com/angular-ui/ui-router/issues/582 https://github.com/angular-ui/ui-router/issues/582

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章