Questions tagged [viewchild]

ViewChild is a decorator for Angular component variables, which allow to bind a template element.

ViewChild is a decorator for Angular component variables, which allow to bind a template element. Official Angular documentation here: https://angular.io/api/core/ViewChild.

437 questions
393
votes
19 answers

@ViewChild in *ngIf

Question What is the most elegant way to get @ViewChild after corresponding element in template was shown? Below is an example. Also Plunker available. Component.template.html:
sinedsem
  • 5,413
  • 7
  • 29
  • 46
326
votes
9 answers

How should I use the new static option for @ViewChild in Angular 8?

How should I configure the new Angular 8 view child? @ViewChild('searchText', {read: ElementRef, static: false}) public searchTextInput: ElementRef; vs @ViewChild('searchText', {read: ElementRef, static: true}) public searchTextInput:…
Patrik Laszlo
  • 4,906
  • 8
  • 24
  • 36
318
votes
11 answers

Angular @ViewChild() error: Expected 2 arguments, but got 1

When trying ViewChild I am getting the error. Error is "An argument for 'opts' was not provided." Both @ViewChild is giving the error. import { Component, OnInit, ElementRef, ViewChild, Output, EventEmitter } from '@angular/core'; import {…
stack overflow
  • 3,205
  • 2
  • 9
  • 7
93
votes
11 answers

@viewChild not working - cannot read property nativeElement of undefined

I'm trying to access a native element in order to focus on it when another element is clicked (much like the html attribute "for" - for cannot be used on elements of this type. However I get the error: TypeError: Cannot read property…
BeniaminoBaggins
  • 11,202
  • 41
  • 152
  • 287
52
votes
4 answers

Calling renderRows() on Angular Material Table

I'm trying to get my Angular Table to refresh after updating the data used in the table. The docs say "you can trigger an update to the table's rendered rows by calling its renderRows() method." but it is not like a normal child component where I…
av0000
  • 1,917
  • 6
  • 31
  • 51
51
votes
2 answers

error TS2554: Expected 2 arguments, but got 1 with @ViewChild

I was using ViewChild as follows: @ViewChild("InternalMedia") localStream; @ViewChild("emoji") mEmoji; Which was working fine till angular-7.x as soon as I upgraded it to angular-8.x it started giving following…
Akhilesh Kumar
  • 9,085
  • 13
  • 57
  • 95
44
votes
6 answers

Angular 2 @ViewChild returns undefined

I've looked at several related posts and documentation, but still can't seem to get expected behavior from @ViewChild. Ultimately, I'm trying to set the scroll position of a div. This element isn't a component, but a normal div in my HTML. To…
Adam
  • 877
  • 2
  • 10
  • 24
41
votes
3 answers

Access nth child of ViewChildren Querylist (Angular)

I'm trying to access the nth child of a viewchildren querylist. Below is my TS: @ViewChildren(PopoverDirective) popovers: QueryList; console.log(this.popovers) The console.log shows changes, first, last, length and _results. How…
user749798
  • 5,210
  • 10
  • 51
  • 85
37
votes
3 answers

Unit testing angular 5 component with @ViewChild

I am using angular 5.2.0. I have a child component import { Component } from '@angular/core'; @Component({ template: `
`, }) export class ChildComponent { public childMethod() { ... } } and a parent component which…
Jacopo Lanzoni
  • 1,264
  • 2
  • 11
  • 25
31
votes
6 answers

@ViewChild returns undefined

For some reason my @ViewChild in my Angular 5 App does not work. I have defined it like this in my component: case-detail.component.html:
I have implemented…
dave0688
  • 5,372
  • 7
  • 33
  • 64
30
votes
4 answers

Angular6 @ViewChild undefined with ngIf

I've a problem using @ViewChild with a component showed through ngIf. I found various solutions but no one usefull for me. This is my main component, consisting of various step (I showed only 2 in code for brevity) with a button for forward…
bigskull
  • 541
  • 2
  • 18
  • 28
28
votes
2 answers

How can I get children elements from QueryList in Angular 2?

I am newbie at Angular2. In my view I have few identical children that are generated in *ngFor.