我正在使用 Ionic Framework 开发一个应用程序,并且有多个 View 。路由名称是 app.view1 和 app.view2。我使用 $state.go("app.view2")
从 Controller 转换到下一个 View ,当我单击返回时,app.view1 的 Controller 不会再次执行,这在我的应用程序中非常重要.
请告诉我如何让 Controller 在我路由到它时执行。
最佳答案
Ionic 缓存 View 以获得更快的性能。它使用了 ng-router
的特性。
Ionic will cache a maximum of 10 views, and not only can this be configured, but apps can also explicitly state which views should and should not be cached.
http://ionicframework.com/docs/api/directive/ionNavView/
有多种方法可以解决这个问题。
选项 1
如果您希望 Controller 在每次显示 View 时都执行,您可以禁用该特定路由的缓存。
$stateProvider.state('myState', {
cache: false,
url : '/myUrl',
templateUrl : 'my-template.html'
})
或
<ion-view cache-view="false" view-title="My Title!">
...
</ion-view>
选项 2
您可以禁用所有 View 的缓存。甚至控制观看次数。我不知道这对你是否有用。
$ionicConfigProvider.views.maxCache(0);
选项 3
我的最爱。您可以使用 $ionicView.enter
事件。这保证了应该执行 View 时需要执行的代码。所以你可以区分代码应该执行一次和每次。我想这会对性能产生积极影响。
用法
$scope.$on('$ionicView.enter', function(){
// Coding
});
祝你好运,编码愉快!
关于javascript - ui.router 没有重新加载 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30991990/