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
5
votes
1 answer

getting 'nativeElement.querySelector is not a function' exception in Angular2

I'm trying to access a div in Angular2 like below: import { Component, ViewChild, ElementRef, AfterContentInit } from '@angular/core'; @Component({ selector: 'main', template: `
mohsen
  • 187
  • 1
  • 3
  • 12
5
votes
1 answer

How to create @ViewChild temporary variable in a method in Angular 2?

I', trying to add a html control dynamically to div and after creating the control I would like to perform operations on it by creating viewchild @ViewChild('dropDownListReference') myDropDownList: typeOfComponent able to create in class but in a…
sudhir
  • 1,387
  • 3
  • 25
  • 43
4
votes
1 answer

ERROR in @ViewChild options must be an object literal in angular 9

I have upgraded my Angular project from Angular version 8 to Angular version 9. it was upgraded successfully but when I am running the project on localhost or when I am trying to build my project it is giving me the following error. ERROR in…
Jayesh Vyas
  • 1,145
  • 3
  • 15
  • 35
4
votes
2 answers

Angular 12 ViewChild ElementRef

In previous versions of angular we could just define a viewchild with an element ref like so @ViewChild('fileInput') fileInput: ElementRef; Now we have to initialize it in the constructor? how do we do this for elementRef if there is no default…
noonecious
  • 63
  • 1
  • 1
  • 6
4
votes
0 answers

Get incorrect offsetTop value in AfterViewInit

My code: Template

Create

Component export class CreatePageComponent implements…
4
votes
3 answers

Access MatDrawer using ViewChild is throwing error in Angular 8

I'm trying to set the behavior of MatSideNav through component file using a property of type MatDrawer in Angular version 8 TypeScript Code: // Declaration @ViewChild('drawer', { static: false }) public drawer: MatDrawer; // Toggle the Side Nav…
B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130
4
votes
1 answer

How to dynamically use chartjs-plugin-annotation with ng2-charts?

I'm creating a statistic page where I need to display several charts, I made a DynamicChartComponent which covers the bar- and line-chart from the demo here(https://valor-software.com/ng2-charts/#DynamicChart). One of the charts needs to display an…
Basvdh
  • 43
  • 4
4
votes
1 answer

Angular unit testing - stubbing/mocking a directive with reference in ViewChild

How do you stub/mock a directive/component that is read as a ViewChild? For example, using the simple directive from angular.io: @Directive({ selector: '[appHighlight]' }) export class HighlightDirective { constructor() { } } Let's say that I…
yoonjesung
  • 1,148
  • 1
  • 9
  • 24
4
votes
1 answer

How to attach click handler to projected content element

I'm using content projection to put a button into a component. And the component: @Component({ selector: 'hello', template: `

Hello {{name}}!

Skyler
  • 777
  • 1
  • 9
  • 34
4
votes
1 answer

Angular ViewChild custom sort selector

I'm learning about angular ViewChild currently, but I seem to be missing something when using ViewChild to select custom reference variables from the DOM. In my template I have two tables:
Broc Broccoli
  • 191
  • 3
  • 11
4
votes
1 answer

ngIf with viewchild

I'm new at angular2 and not professional. I have a parent-child component that I want when user click on a button the selector of child component show something. So it should be condition and also it is used from a child component. Here are my…
Amir Gh
  • 265
  • 1
  • 5
  • 20
4
votes
0 answers

In Ionic 2, how do you get ViewChild to not be null during a unit test?

I'm working on writing unit tests for my ionic 2 app, and I have a @ViewChild call that looks like this: and in my slides.ts I have this: @ViewChild(Slides) slides: Slides; This…
slooker
  • 1,060
  • 2
  • 9
  • 16
4
votes
1 answer

How viewChild can get elements that added with js in angular2?

If a HTML element has been added to the DOM(for example after click on button), How can we access this element? viewChild can't see that. Update 1: More description I've used jquery datatable (jquery.dataTables definitelyTyped version). Base on…
MortezaDalil
  • 352
  • 1
  • 2
  • 16
4
votes
1 answer

Angular 2 - Beta 16 @ViewChild, loadNextToLocation trying to load a component inside another one dynamically?

I am trying to load component inside another one dynamically it was working before but when update to beta 16 it stopped. I read the change log and changed my code but still no success. I logged to console what I get from @ViewChild I suppose to…
Tawfiq abu Halawah
  • 1,214
  • 1
  • 12
  • 20
3
votes
1 answer

Using @View Child in component file getting undefined value of prime-ng paginator

@ViewChild('paginator', {static: true}) paginator:Paginator; this paginator is undefined when I console in ngOnInit function. why? I tried to print prime-ng paginator in component file with using view child.but it's getting undefined.