Is there a way to create an equivalent of the Angular 1.x ngInit
directive in Angular 2?
I am familiar with the ngOnInit
hook, which is recommended for initialization code. The ngInit
directive seems like a quick and declarative way to prototype or fix a component, even though it may not be suitable for well-written production code (but every developer has their own preferences).
If I try something like this in a dummy init
directive:
<p [init]="foo = 1; bar()"><p>
The expression gets evaluated more than once and results in the following errors:
Template parse errors:
Parser Error: Bindings cannot contain assignments
In Angular 1.x, I could achieve this using:
$parse($attrs.init)($scope)
So my question is, how can we leverage and possibly extend the Angular 2 parser to evaluate the foo = 1; bar()
template expression during component initialization?