What is the alternative of ng-init=\"myText=\'Hello World!\'\" in Angular 2 to add in the template, not in the component
Another approach is by using the @Output decorator and EventEmitter:
import {Directive, OnInit, Output, EventEmitter} from '@angular/core';
@Directive({
selector: '[ngInit]'
})
export class NgInitDirective implements OnInit {
@Output()
ngInit: EventEmitter = new EventEmitter();
ngOnInit() {
this.ngInit.emit();
}
}
And then use it like:
...
Demo