什么是AngularJS控制器的生命周期? [英] What is the lifecycle of an AngularJS Controller?

查看:632
本文介绍了什么是AngularJS控制器的生命周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以澄清一个AngularJS控制器的生命周期是什么?

Can someone please clarify what the lifecycle of an AngularJS controller is?


  • 是一个控制器单身,或者创建/销毁的需求呢?

  • 如果是后者,什么触发控制器的创建/销毁?

考虑下面的例子:

var demoApp = angular.module('demo')
  .config(function($routeProvider, $locationProvider) {
    $routeProvider
      .when('/home', {templateUrl: '/home.html', controller: 'HomeCtrl'})
      .when('/users',{templateUrl: '/users.html', controller: 'UsersCtrl'})
      .when('/users/:userId', {templateUrl: '/userEditor.html', controller: 'UserEditorCtrl'});
  });

demoApp.controller('UserEditorCtrl', function($scope, $routeParams, UserResource) {
  $scope.user = UserResource.get({id: $routeParams.userId});
});

例如:

在上面的例子中,当我浏览到 /用户/ 1 ,用户1被加载,并设置为 $范围

In the above example, when I navigate to /users/1,user 1 is loaded, and set to the $scope.

然后,当我浏览到 /用户/ 2 ,用户2被加载。为 UserEditorCtrl 的同一个实例重用,或者说是创造了一个新的实例?

Then, when I navigate to /users/2, user 2 is loaded. Is the same instance of UserEditorCtrl reused, or is a new instance created?


  • 如果这是一个新的实例,什么触发了一审的破坏?

  • 如果重复使用,是如何工作的呢? (即,加载数据的方法似乎在创建控制器的运行)

推荐答案

好吧,其实问题是什么是一个 ngView 控制器的生命周期。

Well, actually the question is what is the life cycle for a ngView controller.

控制器不是单身。任何人都可以创建一个新的控制器和他们从来没有自动销毁。事实是,它通常结合到其基本范围的生命周期。控制器每当其范围被破坏不会自动销毁。然而,摧毁一个潜在的范围后,它的控制器是无用的(至少,在设计上,它应该是)。

Controllers are not singletons. Anyone can create a new controller and they are never auto-destroyed. The fact is that it's generally bound to the life cycle of its underlying scope. The controller is not automatically destroyed whenever its scope is destroyed. However, after destroying an underlying scope, its controller is useless (at least, by design, it should be).

回答您的具体问题,一个 ngView 指令(以及为 ngController 指令)将始终的create一个新的控制器和一个新的范围每次导航发生。而<一个href=\"https://github.com/angular/angular.js/blob/65f5e856a161e7c91b9ebde1360242dc704d0510/src/ngRoute/directive/ngView.js#L179\">last范围将要毁灭,为好。

Answering your specific question, a ngView directive (as well for ngController directive) will always create a new controller and a new scope every time a navigation happens. And the last scope is going to be destroyed as well.

生命周期事件是非常简单的。您的创建事件是控制器本身的建设。只要运行您的code。要知道,当它变得无用(破坏事件),收听范围 $摧毁事件:

The life cycle "events" are quite simple. Your "creation event" is the construction of your controller itself. Just run your code. To know when it gets useless ("destruction event"), listen to scope $destroy event:

$scope.$on('$destroy', function iVeBeenDismissed() {
  // say goodbye to your controller here
  // release resources, cancel request...
})

有关 ngView 具体而言,你能知道什么时候该内容被通过的范围事件时加载 $ viewContentLoaded

For ngView specifically, you are able to know when the content gets loaded through the scope event $viewContentLoaded:

$scope.$on('$viewContentLoaded', function readyToTrick() {
  // say hello to your new content here
  // BUT NEVER TOUCHES THE DOM FROM A CONTROLLER
});

这里是一个Plunker 举证概念(打开你的控制台窗口)。

Here is a Plunker with a concept proof (open your console window).

这篇关于什么是AngularJS控制器的生命周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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