Ionic后退刷新

时间:2023-03-08 18:02:18

版本:Angular 1.5.3、Ionic1.3.2

一 禁用缓存,全页面刷新。

每次前进/ 后退时,控制器都会执行。

1 AngularJS ui-router路由禁用缓存

var app = angular.module('myApp',['ionic']);
app.config(function($stateProvider,$urlRouterProvider){
$stateProvider
.state('index',{
url:'/index',
cache : false,
templateUrl:'template/index.html',
controller:'indexCtrl'
})
$urlRouterProvider.otherwise('/index');
});

2 Ionic view禁用缓存

<ion-view title="index" cache-view="false">
<ion-content>
...
</ion-content>
</ion-view>

3 Ionic 全局禁用缓存

var app = angular.module('myApp',['ionic']);
app.config(function($stateProvider,$urlRouterProvider,$ionicConfigProvider){
$ionicConfigProvider.views.maxCache(0);
});

二 局部刷新

前进到页面,控制器执行;后退到页面,控制器不执行。

(function(angular){
angular.module('myApp')
.controller('indexCtrl',function($scope){
$scope.$on('$ionicView.beforeEnter',function(event,view){
$scope.refreshData(); // 自定义刷新方法
});
});
})(angular);