How to delete a DOM element using Angular 6

I need to implement the functionality of removing a specific block of HTML tags from the DOM when clicked.

<tr *ngFor="let item of uploader.queue">
  <th width="90%">{{ item.file.name}}({{item.file.size/1000000}} MB)</th>
  <th class="text-center" width="10%">
    <mat-icon (click)="item.remove()">delete</mat-icon>
  </th>
</tr>

The .remove() function is functioning correctly in this block.

<tr *ngFor="let items of uploaded_files">
  <th width="90%">{{uploaded_files}} </th>
  <th class="text-center" width="10%">
    <mat-icon (click)="items.remove()">delete</mat-icon>
  </th>
</tr>

However, the .remove() function is not working as expected in this block, resulting in the following error message: _v.context.$implicit.remove is not a function(…)

Answer №1

In case you prefer not to delete an item from the array but simply want to remove an element:

You need to include #item inside the tr tag and then modify items.remove() to item.remove()

For example:

<tr *ngFor="let items of uploaded_files" #item>
    <th width="90%">{{uploaded_files}} </th>
    <th class="text-center" width="10%">
        <mat-icon (click)="item.remove()">delete</mat-icon>
    </th>
</tr>

Answer №2

It is likely that the variable uploaded_files is structured as an array, meaning it may not inherently support a delete function for its elements.

One possible approach to removing elements is by their index:

public removeUploadedFile(index: number): void {
   uploaded_files.splice(index, 1);
}

Additionally,

<tr *ngFor="let items of uploaded_files; let i = index">
  <th width="90%">{{uploaded_files}} </th>
  <th class="text-center" width="10%">
    <mat-icon (click)="removeUploadedFile(i)">delete</mat-icon>
  </th>
</tr>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Discovering new bugs in VSCode Playwright Tests but failing to see any progress

This morning, everything was running smoothly with debugging tests. However, after a forced reboot, I encountered an issue where it seems like the debugger is running, but nothing actually happens. This has happened before, but usually resolves itself. Unf ...

Having trouble with SVG Circles - Attempting to create a Speedometer design

Trying to implement a speedometer in one of the components of my Vue project, but encountering an issue. When I input 0 into my progress calculation for determining the stroke fill, it overlaps with the background circle instead of staying within its bound ...

What is the step-by-step process for generating a tsconfig.json file within an Angular 2

Recently, I completed setting up an Angular2 project with the help of npm. I followed the instructions from this resource: Angular2 Tutorial After successfully generating the package.json file using the npm init command, I realized that there was no speci ...

Conditionally displaying a select element using Ng-if

Is there a way to display content based on the user's selection in an HTML form? Here is how I attempted to do it: <select class="form-control" id="Intervencion" > <option selected disabled></option> <option (click)="show ...

TypeScript encounters difficulty locating the div element

Recently attempted an Angular demo and encountered an error related to [ts] not being able to locate a div element. import { Component } from "@angular/core"; import { FormControl } from "@angular/forms"; @Component({ selector: "main", template: ' ...

Obtaining distinct form control values for replicated form fields with Angular

Issue with Dynamic Form Duplicates: I am currently working with a reactive form that has two fields - name and value. There is an add button that duplicates the form by copying these fields. The challenge I am facing is with updating the values of the dup ...

New substitute for extending the extent in OpenLayers 4

I am currently in the process of migrating my code from OpenLayers 3 to OpenLayers 4 using TypeScript. Previously, I had a code snippet that extended the extent of my map so that all vector shapes I drew would be visible upon loading... angular.forEach(w ...

What is the best way to determine if a variable exists within an array in Angular and JavaScript?

Currently, I am working on a project using Angular 6 with Laravel. In one part of my code, I am fetching an array in the frontend and need to check if a certain variable is present within that array. In PHP, you can easily achieve this using the in_array f ...

You are not allowed to have a union type as the parameter type for an index signature. If needed, consider using a mapped

I'm attempting to implement the following structure: enum Preference { FIRST = 'first', SECOND = 'second', THIRD = 'third' } interface PreferenceInfo { isTrue: boolean; infoString: string; } interface AllPref ...

Passing parameters to an Angular CLI ejected app

Currently, I am utilizing @angular/[email protected]. I have leveraged the new eject feature to modify the webpack configuration. Now, I find myself perplexed on how to pass parameters to my build process. For instance, how can I execute ng build --p ...

Is there a way to programmatically set a dropdown value by using the dropdown value's identifier?

My goal is to have the job name automatically selected in a dropdown based on the previously selected job. To achieve this, I pass the selected job ID to the next page. https://i.sstatic.net/2LxsD.png Currently, the dropdown contains all available jobs. ...

Ensuring the correct type of keys during Object.entries iteration in TypeScript

When using Object.entries(), it returns the correct value types, but the keys are of type string[], which is incorrect. I want TypeScript to recognize my keys correctly. I attempted to use as const on the object, but it did not have any effect. Is there a ...

Utilize Angular Guards and RxJS to coordinate the flow of data between parent and child Guards. Ensure that the Child Guard waits for the parent

Is it possible to validate data in a child component's guard that was fetched from the parent's guard? I have parent components with employees and used a guard to fetch data from the server. Now, I want to verify this data in the child component& ...

What are some strategies for accessing the original values of form components that have not been altered when using ngModel?

I am currently developing a form and I intend to utilize the previous values as "value" in the form. By employing ngModel dynamically, I am able to change some properties. However, I have encountered an issue where the field that remains unchanged by the u ...

Issue with displaying MP4 movies in Angular

I'm trying to display an mp4 video in Angular 9: <video controls (click)="toggleVideo()" preload="none" *ngIf="post.moviePath != null" #videoPlayer> <source [src]="getMovieSanitazePath(post.moviePath ...

How can you position the input cursor at the end of the default text when navigating through fields with the tab key?

I've implemented tab index in the HTML to navigate from one field to another. In the image below, you can see me tabbing from "Revise" to "Link". https://i.stack.imgur.com/vb6L.png However, when I press tab, the default text in the Link field is fu ...

Prettyprint XML in Angular 8+ without using any external libraries

I am working with Angular 8+ and I need to display XML content in a nicely formatted way on my HTML page. The data is coming from the backend (Java) as a string, and I would like to present it in its XML format without relying on any external libraries or ...

Display the modal in Angular 8 only after receiving the response from submitting the form

I am encountering an issue where a pop-up is being displayed immediately upon clicking the submit button in Angular 8, before receiving a response. I would like the modal to only appear after obtaining the response. Can someone assist me with achieving thi ...

The npm audit command flagged an issue with an invalid tag name,

Whenever I run npm audit on a directory containing the project's package.json and package-lock.json, I encounter the following error message: 0 info it worked if it ends with ok 1 verbose cli [ '/home/user/Downloads/node-v10.14.0-linux-x64/bin/n ...

Child component utilizing Angular reactive form

I want to create a reusable input group component that supports both English and German data. My goal is to have the submit button enabled only when all mandatory fields for both EN and DE are filled out. Additionally, I would like to receive all the data ...