简体   繁体   English

离子控制器重新加载$ ​​state.go与param

[英]Ionic controller reloading on $state.go with param

I have an Ionic application that has multiple tabs. 我有一个有多个标签的Ionic应用程序。 To navigate between tabs I use 在我使用的标签之间导航

$state.go('tabname',{params:params}) 

This is all working fine however if I pass a paramater into $state.go it reloads the whole angular controller. 这一切都正常,但是如果我将一个参数传递给$ state.go它会重新加载整个角度控制器。

I put a console.log at the top of my angular controller. 我把一个console.log放在我的角度控制器的顶部。 This logs out when the tab is first loaded (fine) then if I navigate to another tab then navigate back it does not log (fine) but if i navigate to another tab and call 首次加载选项卡时会注销(正常)然后如果我导航到另一个选项卡然后导航回来它不会记录(正常)但如果我导航到另一个选项卡并调用

$state.go('tabname', {paramKey:'paramValue'}) 

it logs (not fine) telling me that the controller thats associated with that tab is being reloaded. 它记录(不正常)告诉我正在重新加载与该选项卡关联的控制器。 This is causing me lots of issues. 这引起了很多问题。 Is there any way of telling the view not to reload the controller? 有没有办法告诉视图不要重新加载控制器?

Here is the $stateProvider .state for the tab in question: 以下是有问题的标签的$ stateProvider .state:

.state('tab.ask', {
    url: '/ask',
    views: {
        'tab-ask': {
            templateUrl: 'templates/tab-ask.html',
            controller: 'AskCtrl'
        }
    },
    params: {
        'paramKey': null
    }
})

First off, there is no way to tell controller to NOT reload on the state change. 首先,没有办法告诉控制器不重新加载状态变化。 As far as I know that is basically how angular works-data binding and so on. 据我所知,基本上是角度如何工作 - 数据绑定等等。 Meaning, all the values in the controller are not available to current scope and a scope is initiated for the current view. 这意味着,控制器中的所有值都不可用于当前范围,并且会为当前视图启动范围。

The best way to store values without using cache is using $services asit follows Singletons design pattern https://docs.angularjs.org/guide/services and the values passed to the services are not changed even when controllers are reloaded. 在不使用缓存的情况下存储值的最佳方法是使用$services然后遵循Singletons design pattern https://docs.angularjs.org/guide/services ,即使重新加载控制器,传递给服务的值也不会更改。

You can find an example on how to create services on https://www.tutorialspoint.com/angularjs/angularjs_services.htm . 您可以在https://www.tutorialspoint.com/angularjs/angularjs_services.htm上找到有关如何创建服务的示例。

You need to update the services each time any event(on tab) so that you can keep track of which page you are on. 每次任何事件(在选项卡上)都需要更新服务,以便您可以跟踪您所在的页面。

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM