2017-10-13 106 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> 
相关问题