What is the alternative of ng-init=\"myText=\'Hello World!\'\"
in Angular 2 to add in the template, not in the component
@Directive({
selector: '[ngxInit]',
})
export class NgxInitDirective {
constructor(
private templateRef: TemplateRef<any>,
private viewContainer: ViewContainerRef) {
}
@Input() set ngxInit(val: any) {
this.viewContainer.clear();
this.viewContainer.createEmbeddedView(this.templateRef, {ngxInit: val});
}
}
a value expression is set via *ngxInit
and published using as
micro-syntax:
<div *ngxInit="3 * i + j as idx">{{idx}}</div>
published as https://www.npmjs.com/package/ngx-init
It is possible by using OnInit Life Cycle hook as below,
Import OnInit from core library
import {Component, OnInit} from '@angular/core'
Implement it to your component class
export class App implements OnInit {
}
Implement the ngOnInit method
ngOnInit(){
this.myText='Hello World!'
}
LIVE DEMO