admin管理员组文章数量:1355615
ngDestroy lifecycle method is not triggering for dynamically created ponent. I'm creating multiple ponent dynamically using ComponentFactoryResolver.
In my dynamically created ponent I'm fetching some data from API and I'm periodically fetching the data for every 5 mins using setInterval method. and I'm clearing the Interval instance inside ngDestroy method , while redirecting to different page, the ponent's ngDestroy is not triggering and the API is triggering even the ponent is not in the view.
This is how I'm creating ponents dynamically.
const factory = this.resolver.resolveComponentFactory(DynamicComponent); // Component Construction
const ref = factory.create(this.injector);
Here is my DynamicComponent which has the functionalities
import { Component, OnInit, OnDestroy } from "@angular/core";
@Component({
selector: "app-dynamic,
templateUrl: "./dynamicponent.html",
styleUrls: ["./dynamicponent.scss"]
})
export class DynamicComponent implements OnInit, OnDestroy {
loopCount: number;
autoRefreshInterval: any;
constructor() {}
ngOnInit() {
this.fetchData();
this.startAutoRefreshLoop();
}
ngOnDestroy(): void {
console.log("Destroying loop"); // ngOnDestroy is not triggering
this.clearAutoRefreshLoop();
}
clearAutoRefreshLoop() {
clearInterval(this.autoRefreshInterval);
}
/*
function for starting the Automatically recall the service for certain period of time
*/
startAutoRefreshLoop() {
console.log("starting loop");
this.loopCount = 10 * 1000;
this.autoRefreshInterval = setInterval(() => {
this.fetchData();
}, this.loopCount);
}
fetchData() {
// FETCHING DATA FROM API CODE ....
}
}
ngDestroy lifecycle method is not triggering for dynamically created ponent. I'm creating multiple ponent dynamically using ComponentFactoryResolver.
In my dynamically created ponent I'm fetching some data from API and I'm periodically fetching the data for every 5 mins using setInterval method. and I'm clearing the Interval instance inside ngDestroy method , while redirecting to different page, the ponent's ngDestroy is not triggering and the API is triggering even the ponent is not in the view.
This is how I'm creating ponents dynamically.
const factory = this.resolver.resolveComponentFactory(DynamicComponent); // Component Construction
const ref = factory.create(this.injector);
Here is my DynamicComponent which has the functionalities
import { Component, OnInit, OnDestroy } from "@angular/core";
@Component({
selector: "app-dynamic,
templateUrl: "./dynamic.ponent.html",
styleUrls: ["./dynamic.ponent.scss"]
})
export class DynamicComponent implements OnInit, OnDestroy {
loopCount: number;
autoRefreshInterval: any;
constructor() {}
ngOnInit() {
this.fetchData();
this.startAutoRefreshLoop();
}
ngOnDestroy(): void {
console.log("Destroying loop"); // ngOnDestroy is not triggering
this.clearAutoRefreshLoop();
}
clearAutoRefreshLoop() {
clearInterval(this.autoRefreshInterval);
}
/*
function for starting the Automatically recall the service for certain period of time
*/
startAutoRefreshLoop() {
console.log("starting loop");
this.loopCount = 10 * 1000;
this.autoRefreshInterval = setInterval(() => {
this.fetchData();
}, this.loopCount);
}
fetchData() {
// FETCHING DATA FROM API CODE ....
}
}
Share
Improve this question
asked Feb 21, 2020 at 10:08
Mohammed IsmailMohammed Ismail
3081 gold badge4 silver badges15 bronze badges
1
- Can you create a minimal working example on StackBlitz, for example? – alexander.sivak Commented Feb 21, 2020 at 10:10
1 Answer
Reset to default 4You need to destroy the dynamically loaded ponent by manually calling : this.ponentRef.destroy();
to trigger ngOndestroy()
Example :
import {
Component,
ViewChild,
ViewContainerRef,
ComponentFactoryResolver,
ComponentRef,
ComponentFactory
} from '@angular/core';
import { DynamicComponent } from './dynamic.ponent';
@Component({
selector: 'app-root',
templateUrl: './app.ponent.html'
})
export class AppComponent {
title = 'app';
ponentRef: any;
@ViewChild('container', { read: ViewContainerRef }) entry: ViewContainerRef;
constructor(private resolver: ComponentFactoryResolver) { }
createComponent(message) {
this.entry.clear();
const factory = this.resolver.resolveComponentFactory(DynamicComponent);
this.ponentRef = this.entry.createComponent(factory);
}
destroyComponent() {
this.ponentRef.destroy(); // you need to call this
}
}
For more information : how-to-dynamically-create-a-ponent-in-angular
版权声明:本文标题:javascript - ngDestroy lifecycle is not Triggering in dynamically created Angular component - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744044534a2581279.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论