I have component (main-cmp
) with rows from database.
For rows I create another component for eg. row-cmp
main-cmp
have requested data from database, and parse it as
<row-cmp *ngFor="let row of data"
[id]="row.id"
[name]="row.name"
[value]="row.value">
</row-cmp>
In row-cmp
I have declare delete()
function who call http request to my backend. Now when response from request is true I want to destroy row-cmp
for selected row. Is this possible ?
To make a component destroy itself when the user navigates away from it, you have to make the component call its ngOnDestroy() lifecycle hook when the page unloads. This is done by leveraging the host page's life cycle.
If you add a component using ViewContainerRef. createComponent() like shown in Angular 2 dynamic tabs with user-click chosen components, then the component can destroy itself when you pass cmpRef to the created component.
Does ngIf destroy component? Angular's ngIf directive does not simply hide and show. It creates and destroys an HTML element based on the result of a JavaScript expression.
Need to remove the component manual just click destroy button and removed all dynamic components. Finally, we are able to do create and destroy dynamic components in our applications.
This is not supported. I'd suggest to add an eventemitter
@Output() delete:EventEmitter = new EventEmitter();
and then add an event handler that removes the item from the data array
<row-cmp *ngFor="let row of data;let i=index" (delete)="data.splice(i,1)"
[id]="row.id"
[name]="row.name"
[value]="row.value">
</row-cmp>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With