使用$ stateProvider的AngularJS / Ionic路由 - 控制器不会在第二次调用状态时重新加载 [英] AngularJS / Ionic routing using $stateProvider - controller is not reloading the second time a state is called

查看:143
本文介绍了使用$ stateProvider的AngularJS / Ionic路由 - 控制器不会在第二次调用状态时重新加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ionic Framework和AngularJS开发移动应用程序,并且我在控制器初始化后没有重新加载时遇到问题。

I'm developing a mobile app using the Ionic Framework and AngularJS and I am having issues with controllers not reloading once they have been initialised.

其中一个状态转换(从'app.postbox-details'' app.audit-questions')应该将参数传递给'app.audit-questions'控制器,但是这个控制器不会自动更新新参数,因为它没有重新加载。

One of the state transitions (from 'app.postbox-details' to 'app.audit-questions') should pass a parameter to the 'app.audit-questions' controller but this controller does not update itself with the new parameter because it isn't reloading.

代码示例

app.js file - config

angular.module('sf-maintenance', ['ionic', 'starter.controllers', 'starter.services', 'ngCordova'])           
.config(function ($stateProvider, $urlRouterProvider, $httpProvider) {
    $stateProvider
       .state('home', {
           url: '/home',
           templateUrl: 'templates/home.html',
           controller: 'HomeCtrl',
       })
       .state('app', { //app state being the side-menu
           url: '/app',
           abstract: true, //means that this state will never be activated directly, users will always go to a child state instead.
           templateUrl: 'templates/side-menu.html',
           controller: 'MenuCtrl'
       })       
       .state('app.postbox-details', {
           url: '/postbox-details',
           views: {
               'menuContent': {
                   templateUrl: 'templates/postbox-details.html',
                   controller: 'PostboxDetailsCtrl'
               }
           }
       })
    .state('app.audit-questions', {
        url: '/audit-questions/:postboxGuid',
        views: {
            'menuContent': {
                templateUrl: 'templates/audit-questions.html',
                controller: 'AuditCtrl'
            }
        }
    })
    $urlRouterProvider.otherwise('/home');
});

controller.js文件(省略了与之无关的代码)

angular.module('starter.controllers', [])    
.controller('HomeCtrl', function ($scope) {
})    
.controller('MenuCtrl', function ($scope) {
})    
.controller('PostboxDetailsCtrl', function ($scope, $ionicLoading, $ionicPopup, $cordovaBarcodeScanner, $state, DataService) {

    $scope.postboxGuid = DataService.getNewGUID();
    //Rest of the controller functions are below
})    
.controller('AuditCtrl', function ($scope, $ionicSlideBoxDelegate, $stateParams, DataService) {

    $scope.auditDetails = {
        postboxGuid: $stateParams.postboxGuid
    };    
});

查看 - 导航代码

执行导航的视图代码全部使用< a> 标签:

The view code to perform the navigations all use <a> tags:


  • 从主视图到邮箱详情视图:< a class =按钮按钮 - 按钮 - 暗图标 - 右离子 - 雪佛龙 - 右href =#/ app / postbox-details>新检查< / a>

  • 从邮箱详情视图到审核问题视图:< ; class =button button-block button-dark icon-right ion-chevron-rightng-click =saveFormData()
    ng-href =#/ app / audit-questions / {{postboxGuid >新审核< / a>

  • From the home view to the postbox-details view: <a class="button button-block button-dark icon-right ion-chevron-right" href="#/app/postbox-details">New inspection</a>
  • From the postbox-details view to audit-questions view: <a class="button button-block button-dark icon-right ion-chevron-right" ng-click="saveFormData()" ng-href="#/app/audit-questions/{{postboxGuid}}">New audit</a>

所以有人知道如何让控制器到重新加载一旦它被初始化或如果我正在解决这个问题,你可以用错误的方式指导我一个有用的方法吗?

So does anybody know how to get controllers to reload once it has been initialised or if I am going about this problem the wrong way could you guide me to a method that will work?

我最近看到了相关问题和@RadimKöhler的回复在这个问题提供了很好的信息,说明为什么在视图上使用 cache:false 可能不是一个好主意,因为表现。

I recently saw a related question and the response by @Radim Köhler pointed to the answer in this question which provides good information on why it may not be a good idea to use cache:false on a view because of performance.

我想我会分享这个,因为在某些情况下,你可以通过使用 Ionic的内置视图生命周期事件,无需禁用缓存视图即可运行代码。

I thought I would share this because in some situations you may benefit more performance-wise by using one of Ionic's built-in view life cycle events to run code without having to disable the view from being cached.

推荐答案

视图是在离子中标准缓存的。可以在视图或stateprovider中配置缓存。

Views are standard cached in ionic. The caching can configured in the view or stateprovider.

http://ionicframework.com/docs/api/指令/ ionNavView /

这篇关于使用$ stateProvider的AngularJS / Ionic路由 - 控制器不会在第二次调用状态时重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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