Implement lifecycle hooks for directive/component controllers (ng2-style) #14030
Description
I open this issue in order to track/discuss the implementation of lifecucle hooks to directive/component controllers.
Context
In our attempt to improve the upgrade path to Angular 2, it makes sense to allow developers structure their directive/component controllers is a way that is as easy as possible to migrate them to Angular 2 (ideally, there should be no modification of the controllers code necessary).
As part of this attempt, it will be useful to implement some of the lifecycle hooks available in Angular 2. (Obviously, it isn't necessary (or possible) to implement all of them, due to differences between ng1/ng2 internals.)
Lifecycle Hooks
-
OnInit
: Implemented inv1.5.0
as$onInit
. -
OnDestroy
: Useful (and probably straightforward to implement). Discussed in 1.5 Feature: Backport ngOnDestroy (as $onDestroy for example) exactly as ngOnInit ($onInit) #14020. -
DoCheck
:Not applicable.Applicable. (See feat($compile): backport $doCheck #14656.) -
OnChanges
:Not applicable.Applicable. -
AfterContentInit
: Not sure how much more useful thanAfterViewInit
(in the context of ng1). -
: Not applicable.AfterContentChecked
-
AfterViewInit
: Useful. Discussed in FormController: attach to component controller? #13991. -
: Not applicable.AfterViewChecked
-
PostLink
: Angular 1 specific.
UPDATE:
Since we can't match the semantics of AfterView/ContentInit
closely enough, we implemented a $postLink
hook instead, which is close (but not the same) and fits ng1 semantics better.