控制器仅初始化一次,即使`$ stateparams`改变 [英] controller only initialized once even if `$stateparams` changed

查看:858
本文介绍了控制器仅初始化一次,即使`$ stateparams`改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始与工作的离子框架的(并因此与的 Angular.js 的为好),我跑与的角UI问题路由器的:一是我注册的状态不重新初始化时,它的 $ stateParams 的变化之一。起初我以为这是因为国家是抽象的,有孩子,所以它永远不会直接激活 - 但即使当我改变了它是非抽象和最深的国家之一(即看到内部的状态时,叶一个树形结构),问题仍然存在。奇怪的是,它似乎并没有在其他国家发生。

I've recently started working with the Ionic Framework (and therefore with Angular.js as well) and I ran into a problem with the Angular UI Router: One of the states I registered is not re-initialized when one of its $stateParams changes. At first I thought it happens because the state is abstract and has children, so it is never activated directly - but even when I changed it to be non-abstract and one of the "deepest" states (i.e. a leaf when seeing the states within a tree structure), the problem persists. The weird thing is that it seems not to happen on other states.

我曾尝试不同的方法,并仔细阅读文档,但是我无法找到问题的原因。

I have tried different approaches and read the documentation carefully, but I could not find the cause for the issue.

以下是code我用的注册状态。国家 app.festival 是我遇到的问题之一:

The following is the code I use to register the states. The state app.festival is the one I'm having the issue with:


  • 当我链接到 app.festival.overview 状态,第一次,例如用URL /节/ 33 /概述,在国家的控制,以及其抽象父控制器 app.festival 的正确初始化

  • 但后来,当我回去别的地方,然后链接到类似 /节/ 21 /概述,只有这个国家的控制器重新初始化,但不它的抽象父控制器(虽然该参数的 festivalId 是它的一部分,并应触发重载我想)

  • 这导致实际节数据(按 festivalId 引用的),这是由 app.festival 状态管理仍然在整个应用相同的,因为基本上它只是初始化一个控制器的实例为 app.festival 不管不同的 $ stateParams

  • when I link to the app.festival.overview state for the first time, for example with URL /festival/33/overview, the the state's controller as well as its abstract parent controller for app.festival are initialized correctly
  • but then, when I go back somewhere else, and then link to something like /festival/21/overview, only this state's controller is re-initialized, but not its abstract parent controller (although the parameter for the festivalId is part of it and should trigger a reload I suppose)
  • this results in the actual festival data (referenced by festivalId) which is managed by the app.festival state remains the same throughout the entire app because basically it just initializes one "instance" of the controller for app.festival regardless of different $stateParams

我之所以需要一个结构,有需要成为以单节(这应该由 app.festival 状态)。

The reason I need that structure is that there needs to be a whole section of pages (states) that deal with a single festival (which should be managed by the app.festival state).

$stateProvider

.state( 'app', {
    url: '',
    abstract: true,
    views: {
        'appContent': {
            templateUrl: 'views/main.html',
            controller: 'MainCtrl'
        }
    }
})

.state( 'app.general', {
    url: '/general',
    abstract: true,
    views: {
        'mainContent': {
            templateUrl: 'views/sections/general.html',
            controller: 'GeneralCtrl'
        }
    }
})

.state( 'app.festival', {
    url: '/festival/{festivalId:int}',
    abstract: true,
    views: {
        'mainContent': {
            templateUrl: 'views/sections/festival.html',
            controller: 'FestivalCtrl'
        }
    }
})

.state( 'app.general.festivals', {
    url: '/festivals',
    views: {
        'generalContent': {
            templateUrl: 'views/pages/general-festivals.html',
            controller: 'GeneralFestivalsCtrl'
        }
    }
})

.state( 'app.general.events', {
    url: '/events',
    views: {
        'generalContent': {
            templateUrl: 'views/pages/general-events.html',
            controller: 'GeneralEventsCtrl'
        }
    }
})

.state( 'app.general.event', {
    url: '/events/{eventId:int}',
    views: {
        'generalContent': {
            templateUrl: 'views/pages/general-event.html',
            controller: 'GeneralEventCtrl'
        }
    }
})

.state( 'app.festival.overview', {
    url: '/overview',
    views: {
        'festivalContent': {
            templateUrl: 'views/pages/festival-overview.html',
            controller: 'FestivalOverviewCtrl'
        }
    }
});

.state( 'app.festival.guide', {
    url: '/guide',
    views: {
        'festivalContent': {
            templateUrl: 'views/pages/festival-guide.html',
            controller: 'FestivalGuideCtrl'
        }
    }
});

这将是巨大的,如果有人可以帮助我在那里,我已经把相当长的一段时间为研究这个问题,但还没有找到一个解决办法。如果您需要更深入地了解了code的特定部分,我会很乐意在这里发布它。

It would be great if anyone could help me out there as I've put quite some hours into researching the issue, but have not found a solution yet. If you need more insight into specific parts of the code, I'll gladly post it here as well.

推荐答案

这是离子的默认行为。它在默认情况下缓存视图和控制器执行以提高性能。你可以用缓存禁用:假

This is the default behaviour of Ionic. It by default caches view and controller execution to improve performance. You can disable it with cache: false:

$stateProvider.state('myState', {
   cache: false,
   url : '/myUrl',
   templateUrl : 'my-template.html'
});

,也可以使用在URL级别禁用它 UI-SREF-OPTS how把重装选项UI的SREF标记

请参阅缓存文档。

这篇关于控制器仅初始化一次,即使`$ stateparams`改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆