Tips for dynamically naming references within a div element in an Angular template loop

I am currently working on a project utilizing Angular 8.3.

In my code, I have a loop using *ngFor where each dynamically added div needs to have a unique reference name.

Here's an example of the code:

<div *ngFor="let item of list; let i = index;">
    <div
        cdkDropList
        #done0="cdkDropList"></div>
</div>

Additionally, there is another drag and drop zone that should receive these elements:

<div
          cdkDropList
          #donesList="cdkDropList"
          [cdkDropListData]="DonesList"
          [cdkDropListConnectedTo]="[done0, done1]"
          class="movie-list"
          (cdkDropListDropped)="onDrop($event)">
            <div *ngFor="let itemList of DonesList" cdkDrag>{{itemList}}</div>
          </div>

My challenge lies in changing #done0 dynamically to #done1, #done2, etc.

Is there a way to achieve this using something like #done{{i}}? I have attempted this but it did not work as expected.

If you have any insights on how to use trackBy in this scenario, please share your advice.

Thank you,

Answer №1

Make a reference to a specific container:

<div *ngFor="let item of list; let i = index;" #doneContainer>
  <div cdkDropList></div>
</div>

In your TypeScript file, you can access different references within a NodeList:

const list = this.doneContainer.childNodes

Alternatively, in a more Angular-oriented approach that automatically updates when needed:

@ViewChildren('doneContainer') list: QueryList<any>;

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

Triggering a CSS class change in Angular based on Firestore events

I want to update a CSS class when new data is received from Firestore. I attempted the following code: this.afs.collection('orders').doc(this.data.id).valueChanges().subscribe(dataset => { console.log(dataset.state); this.state = dataset.s ...

The Angular routerLink parameters are modifying the URL without actually navigating or refreshing the page

When I try to reload the page with different data, the URL changes but the page does not actually reload. In my post.component.ts file: constructor(private postService :PostService, private activateRoute: ActivatedRoute) {} private getPostsbyCategory(){ ...

Rails steers the user back to the RESTful partial created with AJAX

The title may not be very clear, but it encompasses all the elements of the situation at hand. Allow me to elaborate. Within my application, I have a view located at /settings, presented as follows: https://i.sstatic.net/LDV9D.png The code for this view ...

Guide on Applying a Dynamic Color in VueJs 3 Composition API/Vuetify Using CSS

Currently, my project utilizes Vue 3 with the composition API and Vuetify for the UI. I am looking to utilize a color that is already defined in a Vuetify theme variable within my CSS, similar to how I have done it previously in JavaScript. Although I at ...

Display a concealed div when an ng-click event occurs within an ng-repeat loop

$scope.editPostComment = false; Whenever I click on the button, it displays the textarea in all the repeated items instead of just the clicked div! <div class="commentBox" ng-show = "editPostComment"> <textarea name="editor2" ...

Deactivate the submission button when there are no search results in typeahead.js

How can I disable the submit button if there are no search results found? The form should not be submitted in this scenario. I am currently using typeahead.js for my code. $('.typeahead').typeahead(null, { name: 'suburb', disp ...

Setting up Serverless Lambda for a Graphql API with Typescript: Step-by-step guide

Recently, I ventured into the world of AWS and attempted to deploy a GraphQL API written in NodeJS on an AWS Lambda using Serverless. Despite following multiple tutorials that were similar to my project, I encountered difficulties with the handler function ...

Can you explain the significance of the visitFunction error message?

When running [email protected] + [email protected] + [email protected] + [email protected], I encountered an error message. Can anyone help me understand what this error means? I am simply executing: res.render(view, response); Proper ...

Conceal elements on a webpage using Python's Selenium WebDriver through the execution of JavaScript code

I am looking to use a python script to hide elements on a canvas element using javascript. Here is what I have attempted: def hide_elements(): driver = LiveLibrary.get_webdriver_instance() js_script = '''\ ...

Whenever I use Vue JS, instead of receiving my array of objects, I am returned with [__ob__: Observer

Struggling to retrieve data from the database using an API call in my VueJS application. I'm new to VueJS and JavaScript, tested the API with Postman and received the correct JSON response. Current output: [__ob__: Observer] length: 0 __ob__: Observ ...

Steps to resolve the issue of ng-click not functioning in a recursive directive template

I've encountered an issue with using ng-click within the template of a recursive directive. Here is the code snippet: https://jsfiddle.net/qxz7506n/ While clicking the 'root load more button' works fine, the 'child load more button&ap ...

Is there a way to refresh autocomplete/autofill after form submission with a custom JavaScript function?

I've developed a Vue application that includes a form. Once the user clicks on submit, an Ajax request is made using Axios through a JavaScript function. The data being sent is a custom JSON object that I have constructed by combining the information ...

angular use ngFor directive to restrict the number of rows displayed in a table

In my angular 6 project, I am trying to limit the display to just five rows in a table using ngFor. My current code snippet is as follows: <tbody> <tr *ngFor="let item of routines; let i = index"> <td style="display: none"> ...

tips for selecting various API requests based on the selected drop down menu choice

Hey there! I'm looking to enhance my login page by adding a feature that allows users to select from a dropdown menu with different options. Each option will be linked to a specific API, and based on the API response, the user's ability to log in ...

Slide up a row in the table using jQuery

Currently, I am in the process of creating a unique jQuery plugin that enables users to instantly delete records within a table. One key feature I would like to implement is changing the background-color of the deleted row to red and then smoothly sliding ...

Arranging divs using inline-block display. How to adjust the heights consecutively?

After much searching and attempting, I am still unable to achieve a simple task. My goal is to apply display inline-block to some divs while aligning them in a row, similar to the image below: https://i.sstatic.net/iLhLS.png The issue arises when number ...

What is the proper method for activating the lights in a traffic light sequence?

I've been working on creating a traffic light sequence where each time the "change lights" button is pressed, the light is supposed to change. However, I'm facing an issue where the code only displays the red light and doesn't switch to ambe ...

Building Unique Staircases with Three.js Geometry

I have been working on creating a custom three.js geometry for round staircases, but I seem to have made some errors with the vertices and indexes of the steps. Below is an example staircase that utilizes my custom geometry: https://i.sstatic.net/uQXvP.j ...

Dexie: scheduling happenings within a specified date range

Currently, I am utilizing the Dexie library for IndexedDb. As I delve into the API documentation, my focus is on finding a way to select events that fall within two specific dates. It appears that this can be achieved using the .between() method in the fol ...

Error: The src for the image of the double cheeseburger could not be properly parsed by the `next/image` component

Could you kindly take a moment to review this? import Image from "next/image"; import { useState, useEffect } from "react"; import "@/data/data.json"; interface propsData { dish: { id: numbe ...