angularjs控制器实例,UI路由器路由器、控制器、实例、angularjs

2023-09-13 03:55:03 作者:悪魔

在你的控制器被实例化?难道你访问该国的第一次?同时,会发​​生什么,当你再次访问状态,做了新的控制器再次获得实例?

假设我有两个国家,A和B,我把警报语句在状态B.顶部我注意到,如果从状态A到B态B的警示语句将关闭它告诉我,控制了实例化。但是假设我从状态A到B到C,然后回到B,警告语句不会熄灭。但是,如果我从状态A到B到C到B到A到B的警报再次声明熄灭。

下面是我的路线的一部分:

状态A = app.login

状态B = app.pin codeCreate

状态C = app.messagelist

  .RUN($ ionicPlatform,启动) -  GT;  $ ionicPlatform.ready(startup.ionicReady)的.config(googleAnalyticsCordovaProvider,$ stateProvider,$ urlRouterProvider) -  GT;  $ stateProvider    .STATE('应用',{      网址:'/应用程序',      摘要:真实,      templateUrl:'模板/ menu.html',      控制器:'AppController的    })    .STATE('app.pin codeCreate',{      网址:'/针code',      观点:{        menuContent:{          templateUrl:'模板/针code.html',          控制器:'脚$ C $的CController        }      }    })    .STATE('app.login',{      网址:'/登录',      观点:{        menuContent:{          templateUrl:'模板/ login.html的,          控制器:'LoginController中        }     }   })   .STATE('app.messagelist',{      网址:'/ MessageList中的'        观点:{          menuContent:{            templateUrl:'模板/ messagelist.html',            控制器:'MessageListController',            解析:{              活动:(utils的,存储,$州) -  GT;                utils.getActivities(),然后((活动) -  GT。                  store.isUserLoggedIn(真)                 活动              ),(错误) -  GT;              $ state.reload()           }        }      }    }) 
AngularJS实现路由实例

解决方案

视图的特定状态下运行控制器,当你从状态进入该国国家元首或它的后代之一的层次树之外。

在换句话说,如果说,您有以下层次结构:

  A B    / /   AAç        / \\       C1 C2 

然后,从A切换到B。将实例B.然后切换到C(或C1或C2,对于这个问题),然后回到B,不会重新实例B的控制器。

如果您切换到A(或AA),那么A将实例。然后切换回到B将重新实例B点。

因此​​,最有可能在你的情况,C是B. A和B的子状态都在不同的祖先树。

When do controllers get instantiated? Is it the first time you visit that state? also, What happens when you revisit the state, does a new controller get instantiated again?

Assume that I have two states, A and B, and I put an alert statement at the top of state B. I noticed that if go from state A to B state B's alert statement sets off which tells me that the controller got instantiated. But suppose I go from state A to B to C and back to B, the alert statement does NOT go off. However, if I go from state A to B to C to B to A to B the alert statement goes off again.

Here is a part of my routes:

state A = app.login

state B = app.pincodeCreate

state C = app.messagelist

.run ($ionicPlatform, startup) ->
  $ionicPlatform.ready(startup.ionicReady)

.config (googleAnalyticsCordovaProvider, $stateProvider, $urlRouterProvider) ->

  $stateProvider

    .state('app', {
      url: '/app',
      abstract: true,
      templateUrl: 'templates/menu.html',
      controller: 'AppController'
    })

    .state('app.pincodeCreate', {
      url: '/pincode',
      views: {
        menuContent: {
          templateUrl: 'templates/pincode.html',
          controller: 'PincodeController'
        }
      }
    })

    .state('app.login', {
      url: '/login',
      views: {
        menuContent: {
          templateUrl: 'templates/login.html',
          controller: 'LoginController'
        }
     }
   })
   .state('app.messagelist', {
      url: '/messagelist',
        views: {
          menuContent: {
            templateUrl: 'templates/messagelist.html',
            controller: 'MessageListController',
            resolve: {
              activities: (utils, store, $state) ->
                utils.getActivities().then ((activities) ->
                  store.isUserLoggedIn(true)
                 activities
              ), (error) ->
              $state.reload()
           }
        }
      }
    })

解决方案

The view's controller for a particular state runs when you go from state outside of the hierarchy tree of that state to that state or one of its descendants.

In other words, if, say, you have the following hierarchy:

     A     B
    /     /
   AA    C
        / \
       C1 C2

Then, switching from A to B would instantiate B. Switching then to C (or C1 or C2, for that matter), and then back to B, would not re-instantiate B's controller.

If you switch to A (or AA), then A would instantiate. Then switching back to B would re-instantiate B.

So, most likely in your case, C is a child state of B. And A and B are in separate ancestry trees.