how to manually invoke these events? ngOnInit not getting fired for the component injected using the...
up vote
0
down vote
favorite
ngOnInit not getting fired for the component injected using the component factory resolver
@ViewChild('target', {
read: ViewContainerRef
}) target;
@Input() step;
@Input() stepinfo;
cmpRef: ComponentRef < any > ;
private isViewInitialized: boolean = false;
constructor(
private componentFactoryResolver: ComponentFactoryResolver,
private compiler: Compiler,
private cdRef: ChangeDetectorRef
) {}
updateComponent() {
if (!this.isViewInitialized) {
return;
}
if (this.cmpRef) {
this.cmpRef.destroy();
}
let factory = this.componentFactoryResolver.resolveComponentFactory(this.step);
this.cmpRef = this.target.createComponent(factory)
this.cmpRef.instance.data = this.stepinfo;
// (<InjectComponent>componentRef.instance).data = this.step;
// this.cdRef.detectChanges();
}
after few reading i came to know in these case we need to
manually invoke these events. am struggling in how to do that
angular angular6
|
show 4 more comments
up vote
0
down vote
favorite
ngOnInit not getting fired for the component injected using the component factory resolver
@ViewChild('target', {
read: ViewContainerRef
}) target;
@Input() step;
@Input() stepinfo;
cmpRef: ComponentRef < any > ;
private isViewInitialized: boolean = false;
constructor(
private componentFactoryResolver: ComponentFactoryResolver,
private compiler: Compiler,
private cdRef: ChangeDetectorRef
) {}
updateComponent() {
if (!this.isViewInitialized) {
return;
}
if (this.cmpRef) {
this.cmpRef.destroy();
}
let factory = this.componentFactoryResolver.resolveComponentFactory(this.step);
this.cmpRef = this.target.createComponent(factory)
this.cmpRef.instance.data = this.stepinfo;
// (<InjectComponent>componentRef.instance).data = this.step;
// this.cdRef.detectChanges();
}
after few reading i came to know in these case we need to
manually invoke these events. am struggling in how to do that
angular angular6
You've speltngOnInit
incorrectly in 2 places in this question. Are you sure you've not just got a code typo ? Also, where is the component with thengOnInit
in it ?
– Zze
Nov 8 at 20:41
Sorry thats just a typo. i have corrected that thanks for the catch.
– Aji
Nov 8 at 21:12
Check this stack blitz reference - stackblitz.com/angular/…
– Minu
Nov 8 at 21:12
with this approch its working fine for mee too. the problem comes with this code
– Aji
Nov 8 at 21:14
Can you put in the entire code. The other components as well. May be create a stackblitz, so that we can see what is going wrong
– Minu
Nov 8 at 21:19
|
show 4 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
ngOnInit not getting fired for the component injected using the component factory resolver
@ViewChild('target', {
read: ViewContainerRef
}) target;
@Input() step;
@Input() stepinfo;
cmpRef: ComponentRef < any > ;
private isViewInitialized: boolean = false;
constructor(
private componentFactoryResolver: ComponentFactoryResolver,
private compiler: Compiler,
private cdRef: ChangeDetectorRef
) {}
updateComponent() {
if (!this.isViewInitialized) {
return;
}
if (this.cmpRef) {
this.cmpRef.destroy();
}
let factory = this.componentFactoryResolver.resolveComponentFactory(this.step);
this.cmpRef = this.target.createComponent(factory)
this.cmpRef.instance.data = this.stepinfo;
// (<InjectComponent>componentRef.instance).data = this.step;
// this.cdRef.detectChanges();
}
after few reading i came to know in these case we need to
manually invoke these events. am struggling in how to do that
angular angular6
ngOnInit not getting fired for the component injected using the component factory resolver
@ViewChild('target', {
read: ViewContainerRef
}) target;
@Input() step;
@Input() stepinfo;
cmpRef: ComponentRef < any > ;
private isViewInitialized: boolean = false;
constructor(
private componentFactoryResolver: ComponentFactoryResolver,
private compiler: Compiler,
private cdRef: ChangeDetectorRef
) {}
updateComponent() {
if (!this.isViewInitialized) {
return;
}
if (this.cmpRef) {
this.cmpRef.destroy();
}
let factory = this.componentFactoryResolver.resolveComponentFactory(this.step);
this.cmpRef = this.target.createComponent(factory)
this.cmpRef.instance.data = this.stepinfo;
// (<InjectComponent>componentRef.instance).data = this.step;
// this.cdRef.detectChanges();
}
after few reading i came to know in these case we need to
manually invoke these events. am struggling in how to do that
angular angular6
angular angular6
edited Nov 8 at 22:45
asked Nov 8 at 20:15
Aji
9410
9410
You've speltngOnInit
incorrectly in 2 places in this question. Are you sure you've not just got a code typo ? Also, where is the component with thengOnInit
in it ?
– Zze
Nov 8 at 20:41
Sorry thats just a typo. i have corrected that thanks for the catch.
– Aji
Nov 8 at 21:12
Check this stack blitz reference - stackblitz.com/angular/…
– Minu
Nov 8 at 21:12
with this approch its working fine for mee too. the problem comes with this code
– Aji
Nov 8 at 21:14
Can you put in the entire code. The other components as well. May be create a stackblitz, so that we can see what is going wrong
– Minu
Nov 8 at 21:19
|
show 4 more comments
You've speltngOnInit
incorrectly in 2 places in this question. Are you sure you've not just got a code typo ? Also, where is the component with thengOnInit
in it ?
– Zze
Nov 8 at 20:41
Sorry thats just a typo. i have corrected that thanks for the catch.
– Aji
Nov 8 at 21:12
Check this stack blitz reference - stackblitz.com/angular/…
– Minu
Nov 8 at 21:12
with this approch its working fine for mee too. the problem comes with this code
– Aji
Nov 8 at 21:14
Can you put in the entire code. The other components as well. May be create a stackblitz, so that we can see what is going wrong
– Minu
Nov 8 at 21:19
You've spelt
ngOnInit
incorrectly in 2 places in this question. Are you sure you've not just got a code typo ? Also, where is the component with the ngOnInit
in it ?– Zze
Nov 8 at 20:41
You've spelt
ngOnInit
incorrectly in 2 places in this question. Are you sure you've not just got a code typo ? Also, where is the component with the ngOnInit
in it ?– Zze
Nov 8 at 20:41
Sorry thats just a typo. i have corrected that thanks for the catch.
– Aji
Nov 8 at 21:12
Sorry thats just a typo. i have corrected that thanks for the catch.
– Aji
Nov 8 at 21:12
Check this stack blitz reference - stackblitz.com/angular/…
– Minu
Nov 8 at 21:12
Check this stack blitz reference - stackblitz.com/angular/…
– Minu
Nov 8 at 21:12
with this approch its working fine for mee too. the problem comes with this code
– Aji
Nov 8 at 21:14
with this approch its working fine for mee too. the problem comes with this code
– Aji
Nov 8 at 21:14
Can you put in the entire code. The other components as well. May be create a stackblitz, so that we can see what is going wrong
– Minu
Nov 8 at 21:19
Can you put in the entire code. The other components as well. May be create a stackblitz, so that we can see what is going wrong
– Minu
Nov 8 at 21:19
|
show 4 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
When you manually create components using the ComponentFactoryResolver
, you will have to manually call the Angular life cycle events that you are using on them.
Since these methods are public on your component, you can call them like a normal method. In this case, we are wanting to call the OnInit
life cycle event, so it would be like the following:
updateComponent() {
if (!this.isViewInitialized) {
return;
}
if (this.cmpRef) {
this.cmpRef.destroy();
}
let factory = this.componentFactoryResolver.resolveComponentFactory(this.step);
this.cmpRef = this.target.createComponent(factory)
this.cmpRef.instance.data = this.stepinfo;
this.cmpRef.instance.ngOnInit(); // <-- this is where the life cycle event is ran
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
When you manually create components using the ComponentFactoryResolver
, you will have to manually call the Angular life cycle events that you are using on them.
Since these methods are public on your component, you can call them like a normal method. In this case, we are wanting to call the OnInit
life cycle event, so it would be like the following:
updateComponent() {
if (!this.isViewInitialized) {
return;
}
if (this.cmpRef) {
this.cmpRef.destroy();
}
let factory = this.componentFactoryResolver.resolveComponentFactory(this.step);
this.cmpRef = this.target.createComponent(factory)
this.cmpRef.instance.data = this.stepinfo;
this.cmpRef.instance.ngOnInit(); // <-- this is where the life cycle event is ran
}
add a comment |
up vote
1
down vote
accepted
When you manually create components using the ComponentFactoryResolver
, you will have to manually call the Angular life cycle events that you are using on them.
Since these methods are public on your component, you can call them like a normal method. In this case, we are wanting to call the OnInit
life cycle event, so it would be like the following:
updateComponent() {
if (!this.isViewInitialized) {
return;
}
if (this.cmpRef) {
this.cmpRef.destroy();
}
let factory = this.componentFactoryResolver.resolveComponentFactory(this.step);
this.cmpRef = this.target.createComponent(factory)
this.cmpRef.instance.data = this.stepinfo;
this.cmpRef.instance.ngOnInit(); // <-- this is where the life cycle event is ran
}
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
When you manually create components using the ComponentFactoryResolver
, you will have to manually call the Angular life cycle events that you are using on them.
Since these methods are public on your component, you can call them like a normal method. In this case, we are wanting to call the OnInit
life cycle event, so it would be like the following:
updateComponent() {
if (!this.isViewInitialized) {
return;
}
if (this.cmpRef) {
this.cmpRef.destroy();
}
let factory = this.componentFactoryResolver.resolveComponentFactory(this.step);
this.cmpRef = this.target.createComponent(factory)
this.cmpRef.instance.data = this.stepinfo;
this.cmpRef.instance.ngOnInit(); // <-- this is where the life cycle event is ran
}
When you manually create components using the ComponentFactoryResolver
, you will have to manually call the Angular life cycle events that you are using on them.
Since these methods are public on your component, you can call them like a normal method. In this case, we are wanting to call the OnInit
life cycle event, so it would be like the following:
updateComponent() {
if (!this.isViewInitialized) {
return;
}
if (this.cmpRef) {
this.cmpRef.destroy();
}
let factory = this.componentFactoryResolver.resolveComponentFactory(this.step);
this.cmpRef = this.target.createComponent(factory)
this.cmpRef.instance.data = this.stepinfo;
this.cmpRef.instance.ngOnInit(); // <-- this is where the life cycle event is ran
}
answered Nov 8 at 22:31
Daniel W Strimpel
3,2161416
3,2161416
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53215471%2fhow-to-manually-invoke-these-events-ngoninit-not-getting-fired-for-the-componen%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You've spelt
ngOnInit
incorrectly in 2 places in this question. Are you sure you've not just got a code typo ? Also, where is the component with thengOnInit
in it ?– Zze
Nov 8 at 20:41
Sorry thats just a typo. i have corrected that thanks for the catch.
– Aji
Nov 8 at 21:12
Check this stack blitz reference - stackblitz.com/angular/…
– Minu
Nov 8 at 21:12
with this approch its working fine for mee too. the problem comes with this code
– Aji
Nov 8 at 21:14
Can you put in the entire code. The other components as well. May be create a stackblitz, so that we can see what is going wrong
– Minu
Nov 8 at 21:19