Angular / Ionic - 控制器只运行一次
Angular / Ionic - controller only runs once
我通过 Ionic Framework 从 Angular 开始,但我不明白为什么控制器只 运行s 一次,即我改变状态,控制器 运行s ,更改为另一种状态,然后再次返回,控制器不会 运行 第二次。这是我的状态:
$stateProvider.state( 'container.previous', {
url: 'previous',
views: {
main : {
templateUrl : 'views/previous.html',
controller : function( $scope, $cordovaSQLite ){
$scope.firms = [];
$cordovaSQLite.execute(window.db, "SELECT * FROM recent GROUP BY phone ORDER by id DESC").then(function(res) {
for (i = 0; i < res.rows.length; i++) {
$scope.firms.push(res.rows.item(i));
}
}, function (err) {
console.error(err);
});
}
}
},
onStateChangeStart : function(){
backButton = true;
}
});
在另一种状态下,如果您单击与 "firm" 相关的按钮,它会将 "firms" 数据保存到本地存储。上面的状态显示了您之前点击过的公司。但是我不知道如何正确更新 $scope.firms
,因为控制器再也不会 运行。
谁能帮帮我?
Ionic内部有缓存机制。您可以像这样在配置函数中全局禁用缓存:
$ionicConfigProvider.views.maxCache(0);
您不需要禁用整个应用缓存。
如果你想在每次进入时重新运行控制器,你应该在离开之前清除缓存:
$scope.$on("$ionicView.afterLeave", function () {
$ionicHistory.clearCache();
});
你可以把你想要的代码 运行 放在 $ionicView.enter:
controller : function( $scope, $cordovaSQLite ){
$scope.$on('$ionicView.enter', function() {
// code to run each time view is entered
});
...
});
参见"View LifeCycle and Events":https://ionicframework.com/docs/v1/api/directive/ionView/
把这个放在你的离子视图中。
<ion-view cache-view="false">
基于@brandyshea 的回答,我想补充一点,如果您想在一个 area/controller/state 中指定无缓存,并利用其他区域的缓存,您可以简单地使用 cache
在您的 $stateProvider
中为该州设置参数。
Disable cache within state provider
$stateProvider.state('myState', {
cache: false,
url : '/myUrl',
templateUrl : 'my-template.html'
})
或者,您也可以使用其他方法之一:
Disable cache with an attribute
<ion-view cache-view="false" view-title="My Title!">
...
</ion-view>
Disable cache globally
The $ionicConfigProvider can be used to set the maximum allowable views which can be cached, but this can also be use to disable all caching by setting it to 0.
$ionicConfigProvider.views.maxCache(0);
参考文献:http://ionicframework.com/docs/api/directive/ionNavView/ and http://ionicframework.com/docs/api/directive/ionView/
我通过 Ionic Framework 从 Angular 开始,但我不明白为什么控制器只 运行s 一次,即我改变状态,控制器 运行s ,更改为另一种状态,然后再次返回,控制器不会 运行 第二次。这是我的状态:
$stateProvider.state( 'container.previous', {
url: 'previous',
views: {
main : {
templateUrl : 'views/previous.html',
controller : function( $scope, $cordovaSQLite ){
$scope.firms = [];
$cordovaSQLite.execute(window.db, "SELECT * FROM recent GROUP BY phone ORDER by id DESC").then(function(res) {
for (i = 0; i < res.rows.length; i++) {
$scope.firms.push(res.rows.item(i));
}
}, function (err) {
console.error(err);
});
}
}
},
onStateChangeStart : function(){
backButton = true;
}
});
在另一种状态下,如果您单击与 "firm" 相关的按钮,它会将 "firms" 数据保存到本地存储。上面的状态显示了您之前点击过的公司。但是我不知道如何正确更新 $scope.firms
,因为控制器再也不会 运行。
谁能帮帮我?
Ionic内部有缓存机制。您可以像这样在配置函数中全局禁用缓存:
$ionicConfigProvider.views.maxCache(0);
您不需要禁用整个应用缓存。 如果你想在每次进入时重新运行控制器,你应该在离开之前清除缓存:
$scope.$on("$ionicView.afterLeave", function () {
$ionicHistory.clearCache();
});
你可以把你想要的代码 运行 放在 $ionicView.enter:
controller : function( $scope, $cordovaSQLite ){
$scope.$on('$ionicView.enter', function() {
// code to run each time view is entered
});
...
});
参见"View LifeCycle and Events":https://ionicframework.com/docs/v1/api/directive/ionView/
把这个放在你的离子视图中。
<ion-view cache-view="false">
基于@brandyshea 的回答,我想补充一点,如果您想在一个 area/controller/state 中指定无缓存,并利用其他区域的缓存,您可以简单地使用 cache
在您的 $stateProvider
中为该州设置参数。
Disable cache within state provider
$stateProvider.state('myState', { cache: false, url : '/myUrl', templateUrl : 'my-template.html' })
或者,您也可以使用其他方法之一:
Disable cache with an attribute
<ion-view cache-view="false" view-title="My Title!"> ... </ion-view>
Disable cache globally The $ionicConfigProvider can be used to set the maximum allowable views which can be cached, but this can also be use to disable all caching by setting it to 0.
$ionicConfigProvider.views.maxCache(0);
参考文献:http://ionicframework.com/docs/api/directive/ionNavView/ and http://ionicframework.com/docs/api/directive/ionView/