2017-10-13 107 views
1

我有一個離子應用程序,有一些視圖,當我啓動應用程序時,我將轉到我的主視圖,當視圖的控制器初始化時,我將加載一些數據。離子視圖不緩存,控制器重新加載

問題是,當我從該視圖導航時,使用選項卡,該控制器被有時銷燬,所以當我導航回數據時將不得不再次加載。

我已經做了一些測試與「$ ionicView.loaded」「$ ionicView.unloaded」,似乎當視圖被卸載是相當隨機的。

這是我的狀態配置

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

$stateProvider 

    .state('login', { 
    name: "login", 
    url: "/login", 
    templateUrl: "templates/login.html", 
    controller: 'loginCtrl' 
    }) 

    // This is a sidemenu 
    .state('app', { 
    url: '/app', 
    abstract: true, 
    templateUrl: 'templates/menu.html' 
    }) 

    .state('app.hms', { 
    url: '/hms', 
    views: { 
     'menuContent': { 
     templateUrl: 'templates/hms-app.html', 
     controller: 'HmsCtrl' 
     } 
    } 
    }) 

    .state('app.hms-item', { 
    url: '/hms/:hmsId', 
    views: { 
     'menuContent': { 
     templateUrl: 'templates/hms-item.html', 
     controller: 'HmsItemCtrl' 
     } 
    }, 
    params: { 
     item: null 
    } 
    }) 

    .state('app.history', { 
    url: '/history', 
    views: { 
     'menuContent': { 
     templateUrl: 'templates/history-list.html', 
     controller: 'HistoryCtrl' 
     } 
    } 
    }) 
    .state('app.history-item', { 
    url: '/history/:itemId', 
    views: { 
     'menuContent': { 
     templateUrl: 'templates/history-item.html', 
     controller: 'HistoryItemCtrl' 
     } 
    }, 
    params: { 
     itemId: null 
    } 
    }) 

    .state('app.event-new', { 
    url: '/event/new', 
    views: { 
     'menuContent': { 
     templateUrl: 'templates/event-new.html', 
     controller: 'EventCtrl as ctrl' 
     } 
    } 
    }) 

    .state('app.risk-new', { 
    url: '/risk/new', 
    views: { 
     'menuContent': { 
     templateUrl: 'templates/risk-new.html', 
     controller: 'RiskCtrl as ctrl' 
     } 
    } 
    }); 


// if none of the above states are matched, use this as the fallback 
// this is also the first page, 'front page' 
$urlRouterProvider.otherwise('login'); 

}); 

我的選項卡每個html的模板,我需要他們在定義(所以不是登錄頁面,例如),他們的離子含量閉幕後確定:

<div class="tabs tabs-icon-top"> 
<a class="tab-item" href="#/app/hms"> 
    <i class="icon ion-home"></i> 
    Home 
</a> 
<a class="tab-item" href="#/app/event/new"> 
    <i class="icon ion-document-text"></i> 
    Hendelse 
</a> 
<a class="tab-item" href="#/app/risk/new"> 
    <i class="icon ion-document-text"></i> 
    Risikorapport 
</a> 
<a class="tab-item" href="#/app/history"> 
    <i class="icon ion-ios-list"></i> 
    Historikk 
</a> 

什麼會導致視圖的控制器被破壞,當我從它導航?

回答

1

原來,Ionic刪除了「前進」視圖的緩存,所以從前視圖導航時,$ scope將被銷燬。

這是在緩存here

默認情況下說明的,在歷史上航行回來時,「前進」的觀點是從緩存中刪除。如果再次向前導航到相同的視圖,它將創建一個新的DOM元素和控制器實例。基本上,每次都會重置任何前向視圖。這可以通過使用$ ionicConfigProvider進行配置:

因此,配置$ionicConfigProvider$ionicConfigProvider.views.forwardCache(true);固定我的問題

我也看到了,我可以解決我的問題的另一種方式,那就是不要讓我的意見「前進」,使用的href導航到一個新的視圖時,我們可以指定什麼樣的觀點應該是,通過做nav-direction="swap",可用方向是標籤的forward, back, enter, exit, and swap

例如,方向

<a class="tab-item" nav-direction="swap" nav-transition="android" href="#/app/home"> 
    <i class="icon ion-home"></i> 
    Home 
</a> 
相關問題