国家实例化兄弟姐妹之间的混淆

时间:2015-07-05 05:23:29

标签: javascript angularjs angular-ui-router ionic

我已经在Ionic和UI-Router中完成了示例,但我认为这不是Ionic的问题,而是更多关于UI-Router特定的问题。

我总是假设如果在同一级别的层次结构(兄弟姐妹)中切换状态,控制器无论如何都不会重新加载,除非你切换到其他分支。看起来我错了。

以下是一个示例:http://codepen.io/hawkphil/pen/GJxgvE?editors=101

HTML

  <ion-nav-bar class="bar-positive">
    <ion-nav-back-button class="button-icon ion-arrow-left-c">
    </ion-nav-back-button>
  </ion-nav-bar>

  <ion-nav-view></ion-nav-view>


  <script id="templates/tabs.html" type="text/ng-template">
    <ion-tabs class="tabs-icon-top tabs-positive">

      <ion-tab title="Home" icon="ion-home" href="#/tab/home">
        <ion-nav-view name="home-tab"></ion-nav-view>
      </ion-tab>

      <ion-tab title="About" icon="ion-ios-football" href="#/tab/about">
        <ion-nav-view name="about-tab"></ion-nav-view>
      </ion-tab>

      </ion-tab>

    </ion-tabs>
  </script>

  <script id="templates/home.html" type="text/ng-template">
    <ion-view view-title="Home">
      <ion-content class="padding">
        <p>
          <a class="button icon ion-home" href="#/tab/home"> Home</a>
          <a class="button icon icon-right ion-chevron-right" href="#/tab/fact1">
            Fact1
          </a>
          <a class="button icon icon-right ion-chevron-right" href="#/tab/fact2">
            Fact2
          </a>
          <a class="button icon icon-right ion-chevron-right" href="#/tab/fact3">
            Fact3
          </a>
        </p>
      </ion-content>
    </ion-view>
  </script>

  <script id="templates/fact1.html" type="text/ng-template">
    <ion-view view-title="Fact1">
      <ion-content class="padding">
        <p>
          <a class="button icon ion-home" href="#/tab/home"> Home</a>
          <a class="button icon icon-right ion-chevron-right" href="#/tab/fact1">
            Fact1
          </a>
          <a class="button icon icon-right ion-chevron-right" href="#/tab/fact2">
            Fact2
          </a>
          <a class="button icon icon-right ion-chevron-right" href="#/tab/fact3">
            Fact3
          </a>
        </p>
      </ion-content>
    </ion-view>
  </script>

  <script id="templates/fact2.html" type="text/ng-template">
    <ion-view view-title="Fact2">
      <ion-content class="padding">
        <p>
          <a class="button icon ion-home" href="#/tab/home"> Home</a>
          <a class="button icon icon-right ion-chevron-right" href="#/tab/fact1">
            Fact1
          </a>
          <a class="button icon icon-right ion-chevron-right" href="#/tab/fact2">
            Fact2
          </a>
          <a class="button icon icon-right ion-chevron-right" href="#/tab/fact3">
            Fact3
          </a>
        </p>
      </ion-content>
    </ion-view>
  </script>

  <script id="templates/fact3.html" type="text/ng-template">
    <ion-view view-title="Fact3">
      <ion-content class="padding">
        <p>
          <a class="button icon ion-home" href="#/tab/home"> Home</a>
          <a class="button icon icon-right ion-chevron-right" href="#/tab/fact1">
            Fact1
          </a>
          <a class="button icon icon-right ion-chevron-right" href="#/tab/fact2">
            Fact2
          </a>
          <a class="button icon icon-right ion-chevron-right" href="#/tab/fact3">
            Fact3
          </a>
        </p>
      </ion-content>
    </ion-view>
  </script>

  <script id="templates/about.html" type="text/ng-template">
    <ion-view view-title="About">
      <ion-content class="padding">
        <h3>Create hybrid mobile apps with the web technologies you love.</h3>
        <p>Free and open source, Ionic offers a library of mobile-optimized HTML, CSS and JS components for building highly interactive apps.</p>
        <p>Built with Sass and optimized for AngularJS.</p>
        <p>
          <a class="button icon icon-right ion-chevron-right" href="#/tab/navstack">Tabs Nav Stack</a>
        </p>
      </ion-content>
    </ion-view>
  </script>

  <script id="templates/nav-stack.html" type="text/ng-template">
    <ion-view view-title="Tab Nav Stack">
      <ion-content class="padding">
        <p><img src="http://ionicframework.com/img/diagrams/tabs-nav-stack.png" style="width:100%"></p>
      </ion-content>
    </ion-view>
  </script>

JS

angular.module('ionicApp', ['ionic'])

.config(function($stateProvider, $urlRouterProvider) {

  $stateProvider
    .state('tabs', {
      url: "/tab",
      abstract: true,
      templateUrl: "templates/tabs.html"
    })
    .state('tabs.home', {
      url: "/home",
      views: {
        'home-tab': {
          templateUrl: "templates/home.html",
          controller: 'HomeTabCtrl'
        }
      }
    })
    .state('tabs.fact1', {
      url: "/fact1",
      views: {
        'home-tab': {
          templateUrl: "templates/fact1.html",
          controller: 'Fact1TabCtrl'
        }
      }
    })
    .state('tabs.fact2', {
      url: "/fact2",
      views: {
        'home-tab': {
          templateUrl: "templates/fact2.html",
          controller: 'Fact2TabCtrl'
        }
      }
    })
    .state('tabs.fact3', {
      url: "/fact3",
      views: {
        'home-tab': {
          templateUrl: "templates/fact3.html",
          controller: 'Fact3TabCtrl'
        }
      }
    })
    .state('tabs.about', {
      url: "/about",
      views: {
        'about-tab': {
          templateUrl: "templates/about.html"
        }
      }
    })
    .state('tabs.navstack', {
      url: "/navstack",
      views: {
        'about-tab': {
          templateUrl: "templates/nav-stack.html"
        }
      }
    });


   $urlRouterProvider.otherwise("/tab/home");

})

.controller('HomeTabCtrl', function($scope, $rootScope) {
  console.log('Home');

  $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){ 
    console.log('State change from: ' + fromState.name + ' to: ' + toState.name);
  });
})

.controller('Fact1TabCtrl', function($scope) {
  console.log('Fact1');
})

.controller('Fact2TabCtrl', function($scope) {
  console.log('Fact2');
})

.controller('Fact3TabCtrl', function($scope) {
  console.log('Fact3');
});

这是州的树:

        Home
     /   |   \
Fact1  Fact2  Fact3

以下是流程(打开控制台日志):

  • 我从主页开始 - &gt;事实1 - &gt;事实2 - &gt; Fact3 即可。一切都按预期发生,以初始化所有控制器。

  • 然后它从 Fact3 - &gt;中变得棘手事实1 (没有任何反应) - &gt;事实3 (再次) - &gt;事实2 (只是为了确保没有实例化控制器) - &gt;事实3 (再次,但 BOOM Fact3控制器现在突然实例化 - &gt; 为什么?

console.log输出

Home
State change from: tabs.home to: tabs.fact1
Fact1
State change from: tabs.fact1 to: tabs.fact2
Fact2
State change from: tabs.fact2 to: tabs.fact3
Fact3
State change from: tabs.fact3 to: tabs.fact1
State change from: tabs.fact1 to: tabs.fact3
State change from: tabs.fact3 to: tabs.fact2
State change from: tabs.fact2 to: tabs.fact3
Fact3

所以现在我比以往任何时候都更困惑:

  1. 为什么Fact3被实例化了?

  2. 控制器在兄弟姐妹州实例化的确切规则是什么?

  3. 那么控制器初始化的最佳做法是什么?由于我有两个场景:(a)在app运行时期间初始化一次且仅一次,(b)每次返回状态时重新初始化,无论状态何时以及如何,

1 个答案:

答案 0 :(得分:1)

看起来像导致所有这些的视图缓存。

$ionicConfigProvider.views.maxCache(0);

http://codepen.io/hawkphil/pen/OVvzmg

基本上默认情况下,ui-router会 REFRESH 所有控制器兄弟姐妹:http://plnkr.co/edit/OAB98MvP8NKmskcJkxtd?p=preview

所以我原来的想法是错的(许多其他人也是如此)。