UI-Router:如何在切换视图时保留视图

时间:2016-09-26 04:10:32

标签: jquery angularjs angular-ui-router

我正在使用angular 1.x和ui-router 0.3.x开发一个webapp。当我切换到新的视图状态时,将实例化相应的控制器(数据状态将被重置)。从下面的代码片段中可以看到,每次切换到/logview时都会创建终端(实际上这是jquery.terminal),并且当视图状态更改为其他内容时将被销毁。

angular.module('app')
  .config(['$stateProvider', function($stateProvider) {
    $stateProvider.state('logview', {
      url: '/logview',
      templateUrl: 'views/logview/logview.html',
      controller: 'LogViewCtrl'
    });
  }])
  .controller('LogViewCtrl', ['$scope', function($scope) {
     var terminal = $('#terminal').terminal();
     console.log(terminal created');
     $scope.$on('$destroy', function() {
      terminal.destroy();
    });
  }])
;

我想保留logview并让它在后台运行,这样当我再次切换回logview时,我可以看到自webapp启动以来的所有日志消息..

更新1:我在SO周围搜索,这个question的答案非常符合要求,即使用ng-show来显示/隐藏日志视图。这将是一种方式。但是我需要非常仔细地设计布局,因为我使用kendo-splitter。它将使用绝对容器的宽度和高度。隐藏的div会使计算混乱。

3 个答案:

答案 0 :(得分:2)

在ui-route中,状态和控制器的基本原理是一次又一次地加载和销毁。您可以使用Phil的建议(使用服务)。我曾经遇到过类似的问题。我想出了一个技巧。控制器加载时,只需定义$ rootscope对象。然后每次控制器加载时检查对象是否存在/已定义。如果定义,请不要执行您的代码。否则定义$ rootscope对象并执行代码。

angular.module('MyApp',['ngMessages'])
.controller('AppCtrl', function($scope,$rootscope) {
if (!$rootscope.myCtrl){
        $rootscope.myCtrl = 'exist';
        // Code you don't want to execute in this controller again and again goes here.

        var terminal = $('#terminal').terminal();
        console.log(terminal created');
        $scope.$on('$destroy', function() {
            terminal.destroy();
        };
    // Code you want to execute in this controller goes here.
});

angular.module('MyApp',['ngMessages']) .controller('AppCtrl', function($scope,$rootscope) { if (!$rootscope.myCtrl){ $rootscope.myCtrl = 'exist'; // Code you don't want to execute in this controller again and again goes here. var terminal = $('#terminal').terminal(); console.log(terminal created'); $scope.$on('$destroy', function() { terminal.destroy(); }; // Code you want to execute in this controller goes here. });

我认为这将满足您的目的。

答案 1 :(得分:2)

在谷歌上搜索,包括从ui-router的社区检查some discussion后,我终于找到了一个名为ui-router-extras的优秀ui-router插件,它提供了一个神奇的旗帜var data = " 1234567890" ,所以我现在坚持sticky=true

答案 2 :(得分:1)

使用state保持Ui.RouterSubscriptionManager subscriptionManager = (SubscriptionManager)getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); List<SubscriptionInfo> subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList(); if (subscriptionInfoList != null && subscriptionInfoList.size() > 0) { for (SubscriptionInfo info : subscriptionInfoList) { String carrierName = info.getCarrierName().toString(); String mobileNo = info.getNumber(); String countyIso = info.getCountryIso(); int dataRoaming = info.getDataRoaming(); } } 的好方法是实施嵌套路线https://github.com/angular-ui/ui-router/wiki/nested-states-%26-nested-views

相关问题