How can I retrieve the specific element of *ngFor loop within the component.ts

Currently, I am utilizing the angular2-swing library and have integrated the following code into my template:

  <ul class="stack" swing-stack  [stackConfig]="stackConfig"  #myswing1 (throwout)="onThrowOut($event)">
    <li swing-card #restaurantCards [ngClass]="restaurant.restaurant" *ngFor="let restaurant of restaurants">

      <app-restaurant-card [restaurant]=restaurant.restaurant>
      </app-restaurant-card>
      <div class="overlay">
        YUM!
      </div>  
    </li>
  </ul>

The following is the onThrowOut($event) method:

onThrowOut(event: ThrowEvent) {

    if(event.throwDirection == Direction.RIGHT){
      this.restaurantService.addToLikes(event.target.getAttribute('ngClass'))
    }
    //hide element once thrown out
    event.target.setAttribute("style", "visibility: hidden; transition: .1s; ");
  }

Unfortunately, when attempting to retrieve the attribute, it returns null. Is there a way to access the element from within the for loop? Thank you

Answer №1

ngClass is accessible as class in the DOM. Update your code to use

event.target.getAttribute('class')
:

onThrowOut(event: ThrowEvent) {

  if(event.throwDirection == Direction.RIGHT){
    this.restaurantService.addToLikes(event.target.getAttribute('class'));
  }
  ...
}

Also keep in mind that, now id will be a string, so you may need to convert them to number.

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

How to retrieve multiple checked values from checkboxes in Semantic UI React

Trying to enable users to select multiple checkboxes in react using semantic-ui-react. I am new to react. Here is my code: class ListHandOver extends React.Component { state = { data: {} } handleChange = (e, data) => { console.log( ...

Angular is facing an issue where the Observable cannot be assigned to the specified interface

I have the following interface and model that I am working with. My goal is to retrieve an observable after making an API call. Here is the code: export class RegistrationListResultModel extends TaskResultModel { Users!: UserRegisterModel[]; } export ...

Creating a custom extended version of Angular2 Http does not automatically provide injection of services

I'm struggling to understand this issue. I've created a custom class that extends Angular's Http class. import { Injectable } from '@angular/core'; { Http, ConnectionBackend, RequestOptions, RequestOptionsArgs, ...

Struggling to identify the origin of a TypeError while running ng test

While running ng test in my Angular 7.2.1 project, I encountered a type error that seems fixable: Uncaught TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable. thrown The i ...

Transfer responsibilities of events to the canvas and then fetch the Element in the handler

Currently, I am utilizing the Raphaël library to create a network graph, where nodes are depicted as circles. Users have the ability to dynamically add nodes by clicking on the canvas. When a new node is added, an Element object is pushed into both a Set ...

Ways to fix the bootstrap image zoom issue

Here is my code snippet: https://i.sstatic.net/BJoAP.png Currently, I am attempting to ensure that the thumbnails adjust to the background zoom. I have been struggling to remove the black borders surrounding the thumbnails. If anyone can provide assista ...

The br tag in HTML cannot be utilized in conjunction with JavaScript

I am currently working on a project involving HTML and JavaScript. I have a "textarea" where data is inserted into the database upon pressing the "Enter key." However, I am encountering two issues: Currently unable to save data like "Lorem Ipsum" (the ...

Trigger event in App.component.ts after child view initialization in Angular 8 and higher versions

I've been encountering an issue while trying to incorporate HubSpot page tracking into my Angular 8+ application. Currently, I'm tapping into the NavigationEnd event on my router within app.component.ts. The problem arises when each child compone ...

What steps can be taken to ensure that a second Pinia plugin waits for the completion of the

I am currently developing a Vue application with Pinia as the state manager. I have created two plugins - one for authentication and another to set up my API instance. The second plugin relies on the token obtained from the first plugin. Upon analyzing th ...

Why does my camera suddenly switch to a rear-facing view when I begin my Zoom meeting?

I am facing an issue with my camera function where it initially starts off facing backwards, but as soon as I perform the first scroll, it flips around and works correctly. Please note that I am a beginner in coding. Kindly be aware that there is addition ...

How to Display an HTML Page or DOM Element on the Surface of a Cube using Three.js?

Currently, I am working on customizing the sample file canvas_geometry_cube.html that comes with the Three.js package. My goal is to replace each face of the cube with an HTML page or DOM element (preferably DOM element). While exploring, I came across P ...

Discovering the keycode for the GO button in JavascriptDiscovering the keycode

Can anyone help me figure out how to find the keycode for the GO button using Javascript in an Android browser? ...

Issues arise due to data inconsistency stemming from the combination of a for loop and .map() function within the BACK4APP

I seem to be facing a challenge with data consistency caused by the for (const object of results) {} loop in the Sandbox Link at line41. The issue is that the results are displayed as a single result after using the .map() method. However, when I perform a ...

Tips on personalizing the vue-filemanager interface within Laravel

I'm currently utilizing the Laravel file manager package from here, which provides a pre-compiled JS file or allows for direct use of the vue-component through npm from here. Unfortunately, in both options, the front-end is not customizable. I have i ...

Having trouble triggering a click event with JQuery

I have a hidden link for downloading Audio Files that I want the user to access using a button. However, I am having trouble triggering the click event. Below is the HTML code: <a class="APopupDown" data-icon="home" id="DownloadFile" href="http://yaho ...

confronting #tackling challenges while launching a next.js web application

While trying to deploy my next.js app, I encountered the following issue with #fillNegs in the tailwind node_modules folder. My setup includes node.js version 12.22.0, next.js version 11, and Tailwind version 2.0.2. module.image.null_resource.push (local ...

When a JSON response contains an array of objects with only one element, it is received as a single object

When sending a response from my resource as an array of objects, I encounter an issue where if the array only contains one element, the response is transformed into a single object instead of an array with one object, preventing me from looping through it. ...

Disable the function when the mouse is moved off or released

My custom scrolling implementation in a ticker using Jquery is due to the fact that standard scrolling doesn't function well with existing CSS animations. The goal is to enable movement of the ticker when clicking and dragging on the controller, a div ...

There seems to be a problem with the [at-loader] node_modules@typesjasmine

My webpack build suddenly started failing with no package updates. I believe a minor version change is causing this issue, but I'm unsure how to resolve it. Can someone provide guidance on what steps to take? ERROR in [at-loader] node_modules\@t ...

What is causing the error message to appear even though the element has been correctly defined? - TypeError: Unable to access the value property of null

Objective: Obtain the value of an HTML element in TypeScript (Angular) Issue: Error: Uncaught (in promise): TypeError: Cannot read property 'value' of null Error Message: TypeError: Cannot read property 'value' of null at UserRegi ...