Exploring nested traversal within a list of DOM queries in Angular

During my development process, I faced a unique challenge that involved parent and child checkboxes at the same level. The desired behavior was to have children checkboxes automatically checked when the parent is checked, and vice versa. Although my HTML and component files seem to be functioning properly, there is a glitch when toggling between different parents after selecting children checkboxes. I welcome any suggestions for solving this issue!

import { Component } from '@angular/core';
import { Input, OnInit, OnDestroy, ViewChildren, ViewChild, ElementRef, QueryList } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';
    @ViewChildren('fundMenu') public fundMenu: QueryList<any>;
  @ViewChildren('accountMenu') public accountMenu: ElementRef;
  @ViewChild('allchkbox') public allchkbox: ElementRef;
  fun = false;

  // more code...

   public checkAllAccount(account: any, selectToggle: any, val: any):void {
    if(val === 'ALL') {
    //this.accountMenu = this.selectAllParentCheckBox(this.accountMenu, false, this.allchkbox);
   // this.fundMenu = this.selectAllParentCheckBox(this.fundMenu, false, this.allchkbox);
    } else {
      console.log(this.fundMenu);
      console.log(this.accountMenu);
      this.fundMenu = this.selectAllCheckBox(this.fundMenu,false,val);
    }
  }

  // additional methods...

}
<hello name="{{ name }}"></hello>
// more HTML code...

https://i.sstatic.net/gGnbt.png

Answer №1

It appears that you are reassigning the i variable in your template code. Initially, it is defined as:

<div id='' class="" *ngFor='let item of rorList;let i =index'>
<input (click)="checkAllAccount(item.key,i,item.key)" #accountMenu value={{item.key}} type="checkbox">      

(By the way, it's recommended to use double quotes for attributes in templates.)

Later on, you have:

<div  *ngFor='let value of item.arr,let i =index'>
  <div  *ngFor='let rorItem of value.performanceType,let i =index'>

It seems like the i variable is being overwritten and causing issues with the state.

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

Testing an attribute directive in Angular 2 through unit testing

I am currently in the process of unit testing a custom maxlength directive with an Input parameter. Here is the code for the directive: import { Directive, Input, HostListener } from '@angular/core'; @Directive({ selector: '[limitTo]&apos ...

Tips for using MatTableDataSource in a custom Thingsboard widget

After creating multiple custom Thingsboard widgets, I've discovered that I can access a significant portion of @angular/material within my widget code. While I have successfully implemented mat-table, I now want to incorporate pagination, filtering, a ...

Ensure the safety of TypeScript class decorators by specifying the constructor function type

Consider the following decorator: export function MyDecorator(constructor: new(someService: SomeService, ...args) => ISomeInterface) { when incorporating it within an Angular component, I require the compiler to verify that the service injection is not ...

Validation of Hotel Amenity Operating Hours in Angular 2

I am a beginner in angular and I could use some guidance. I have a scenario where there are two fields for the open and close time of hotel amenities. Currently, there is no validation in place. The requirement is that until the user selects none of them, ...

The element 'PROGRAM_ID' is not recognized within the 'import @metaplex-foundation/mpl-token-metadata' type

I am currently in the process of creating a token within the Solana network, but I've encountered a particular issue. I have successfully installed @metaplex-foundation/mpl-token-metadata and integrated it into my code; however, an error is persisting ...

Confirm the object received from the API and assign default values

Seeking to extract data from an API and verify if all fields are strings, but if they are missing I aim to assign default values. My intention was to utilize the yup library to validate the object accordingly, ensuring that the returned function is prope ...

Using Angular with Spring Boot: Angular Service sending Http requests to localhost:4200 instead of the expected localhost:8080

I am currently working on a project that involves Angular and Spring Boot. In this setup, the Angular project has services that make Http requests to the Spring Boot app running on port 8080. However, I have encountered an issue where one of the services i ...

What causes a union with a conditionally assigned property to lead to more relaxed types than anticipated?

Take a look at this TypeScript code snippet: const test = Math.random() < 0.5 ? { a: 1, b: 2 } : {}; Based on the code above, I would assume the type of object 'test' to be: const test: { a: number; b: number; } | {} This is the most str ...

Unable to fetch Angular signal from service

I have been working on utilizing a service in order to store the selected division object and then access it in the detail form. Here is the code snippet for the service: private currentDivision = signal<Division>(new Division(), undefined); setC ...

Discover the myriad of possibilities created by combining arrays

I am working on a code snippet that aims to generate an array containing all possible combinations between two or more arrays. However, I am encountering a specific issue. getCombn(arr: string | any[], pre?: string | undefined) { pre = pre || ' &a ...

Expanding interfaces dynamically in Typescript

Currently, I am facing a challenge while attempting to integrate an existing React Native module equipped with the following props: useComponent1: boolean useComponent2: boolean This is how the implementation looks like: render(){ if(useComponent1){ ...

Unit testing with Jest in TypeScript for mocking Express and Mongoose

I've been struggling to find comprehensive resources on using jest.fn() to mock TypeScript classes and their methods, like express' Request, Response, NextFunction, and the save() method on a mongoose model. For instance, suppose I have the foll ...

What steps are involved in setting up a dropdown side navbar?

Is there a way to automatically activate the next dropdown in the side navbar when I click on the 'Next' button? And similarly, can we open the previous dropdown in the side navbar when clicking the 'Previous' button? ...

While trying to update Angular 5 to 6, I am encountering an incompatible peer dependency error when utilizing the ng update @angular/core command

I have been encountering issues while trying to upgrade my Angular app from version 5 to version 6 by following this guide. After successfully running the commands below: npm install -g @angular/cli npm install @angular/cli ng update @angular/cli An err ...

Deactivating specific radio options with Angular Formly

Is there a way to disable a specific Radio Button option, for example the first one, using Angular Formly fields? options: [ { value: 1, label: 1, }, { value: 2, ...

Reinforced.Typings does not generate fully qualified names (FQN) for types when UseModules is set to true or false

Overview: Within my solution, I have two projects: ProjA and ProjB. In ProjA, I've defined a custom RTConfigure method to streamline the configuration process for translating C# to TypeScript using the Reinforced.Typings tool. Meanwhile, ProjB houses ...

New behavior in Vue 3: defineEmits is causing issues with defineProps data

Currently, I am working with Vue 3 and TS 4.4. In one of my components, I am using defineProps to define prop types. However, when I try to add defineEmits, VS Code starts indicating that my props variable is not recognized in the component template. Below ...

What causes the HTML to not evaluate the values when the pipe is used?

I am currently utilizing a pipe for currency conversion, ensuring that the HTML values remain unevaluated. Let's take a look at the following pipe: transform(value: number, selectedCurrency: string, baseCurrency: string): number { if (selectedCu ...

Issues with property binding in Angular are causing problems

Suppose the app component has a variable defined for the value in the input field. However, every time the button event is triggered, the string is printed as empty and the binding does not seem to work at all. export class AppComponent { numVal =1235; ...

Parsing of the module failed due to an unexpected character appearing when trying to insert a TTF file into the stylesheet

As a newcomer to Angular, I recently completed the tutorial and am now working on my first app. While trying to add an OTF file, everything went smoothly. However, when I made a change to my app.component.css file and included this code snippet: @font-fa ...