obtain a list of elements using ViewChild attribute

In Angular 8, I have created a generic form with a form array.

I am trying to retrieve all elements that contain the ViewChild id.

However, when I try to output the ViewChild element using console.log, it only displays one element instead of a list. How can I get it to display a list of elements?

What could be causing this issue? How can I fix it?

HTML:

<ngx-mat-file-input #upload disabled="true" class="uploadFild" (change)="showPreview($event,i)" appearance="outline"
                            formControlName="file" type=file>
                        </ngx-mat-file-input>

TS:

@ViewChild('upload', { static: false }) pRef: ElementRef[];

function():void {
    this.pRef['disabled'] = false;
    console.log(this.pRef);        
}

Answer №1

Instead of using @ContentChildren, try using @ViewChildren in your Angular component.

See an example below:

import {AfterViewInit, Component, Directive, QueryList, ViewChildren} from '@angular/core';

@Directive({selector: 'child-directive'})
class ChildDirective {
}

@Component({selector: 'someCmp', templateUrl: 'someCmp.html'})
class SomeCmp implements AfterViewInit {
  @ViewChildren(ChildDirective) viewChildren !: QueryList<ChildDirective>;

  ngAfterViewInit() {
    // viewChildren property is now accessible
  }
}

For more information, refer to the official Angular documentation here: https://angular.io/api/core/ViewChildren

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

What could be the root of the error message "Outlet is not activated"?

As I work on developing a workflow with 4 steps in my web application, I encounter an issue when navigating from step 1 to step 4 and then back to step 1 without reloading the application. Upon repeating the path from step 1 to step 4 for the second time, ...

Tips for displaying a gallery of 5 images in a 2-row slider using bxslider

I've been attempting to create a unique image slider using bxslider. My goal is to have a 2-row slider with 5 images displayed in each row. However, I'm encountering difficulties when I try to integrate the HTML, CSS, and JavaScript code. Despit ...

Customizing the items-per-page selection in the v-data-table component with Vuetify

I have a data table component footer with a select option that I want to update the values of. Can anyone guide me on how to achieve this? Is there a way to customize the numbers in the select option? https://i.sstatic.net/I5HCA.png ...

Gathering information from an HTML form and displaying it through JavaScript by utilizing Bootstrap's card component

I've been working on integrating form data from my index.html page into a card component using Bootstrap. I've successfully collected the data in the backend, but now I'm struggling to display it on the frontend as a card component. Any help ...

Leverage jQuery to Retrieve Text and Modify

My Content Management System automatically generates a time stamp for when a page was last updated. However, the format it provides is not ideal for my needs. I would like the date to be displayed in the US Standard way - May 24, 2013, without including th ...

A guide on implementing the useEffect hook with forwardRef in TypeScript

Here is a piece of code I am working on: import React from 'react'; type shapeTable = { data: string[][]; onMount?: (tableWidth: string) => void; }; type Ref = HTMLTableElement; const Table = React.forwardRef<Ref, shapeTable>(( ...

Display the properties of the nested object

I am trying to extract and print the postal_code value from the JSON file provided below: { "results" : [ { "address_components" : [ { "long_name" : "286", "short_name" : "286", "t ...

Issues with Rock Paper Scissors Array in Discord.js V12 not functioning as expected

I'm currently working on implementing an RPS game in my Discord bot. I want to create a feature where if the choice made by the user doesn't match any of the options in the list, it will display an error message. Here is the code snippet that I h ...

How can one include a URL as a "URL parameter" when using Express?

In my Node.js application, I've set up a router to listen for requests at api/shorten/: router.get('api/shorten/:longUrl', function(req, res, next) { console.log(req.params.longUrl); } When I enter something like: http://l ...

When utilizing Reactjs, accessing an element by its id becomes challenging when the element is nested within a map() function

Currently, I am encountering an issue related to reactjs. In my scenario, I have a requirement to compare the height of the screen with a specific div to determine its maximum width. The challenge lies in the fact that the particular div I need to analyz ...

Extend the express request object with Typescript and then export the modified object

Seeking to enhance the Request object from express with custom fields using typescript. Based on this particular source, I created a file named @types/express/index.d.ts containing the following code : import { MyClass } from "../../src/MyClass" ...

Having difficulty implementing NG Zorro into the project because of a dependency issue

While attempting to integrate the NG Zorro library into my Angular project, I encountered an issue when running ng add ng-zorro-antd. The error message displayed was: code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While ...

The necessary data is missing in the scope of the callback function

I'm facing an issue with a callback function's variable losing its scope. Consider the following simplified array of two objects: const search = [{socket: new WebSocket('ws://live.trade/123')}, {socket: new WebSocket( ...

Using Jquery to retrieve data in sections from a server and continuously add it to a file on the client side

I have a large dataset stored in JSON format on a Postgres Server with hundreds of thousands of rows. To prevent memory overload on the server, I need to provide users with the ability to download the data in chunks rather than all at once. This requires a ...

Basic exam but located in a place that is not valid

Here is a test I am working on: // import {by, element, browser} from "protractor"; describe('intro', () => { beforeEach(() => { browser.get(''); }); it('should have multiple pages', () => { let buttonOn ...

What methods can be used to customize the font and background color of a website for different user groups?

Trying to incorporate a template into my project. My client has requested the following: The regular user area should feature a blue background. The professional user area should have an orange background. Is there a way to set up a condition to change ...

How can I trigger a PHP function by clicking a button on a PHP page that has already been loaded?

While I've come across a variety of examples, I haven't been able to make them work for the simple task I need to accomplish. The code in these examples seems overly complex compared to what I require. In essence, I have a form that processes dat ...

Refreshing the page allows Socket.io to establish multiple connections

I've been working on setting up a chatroom, but I've noticed that each time the page refreshes, more connections are being established. It's interesting because initially only one connection is created when I visit the chat room page. Howeve ...

Click on the button to add a new question and watch as another row magically appears

Working with ReactJS My goal is to display 10 rows by default, followed by a button labeled "Add a new question" which would create the 11th row. View current row image here Currently, only one row is being shown [referencing the image below]. I aim to ...

Comparing Node.js and Angular for Hyperledger Composer Development: Which is the Better Choice?

When creating applications with Hyperledger Composer, there is a common solution architecture that looks like this: https://i.sstatic.net/GS3BT.png The documentation also suggests an alternative architecture that utilizes Node.js (check it out here): ht ...