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
8
votes
4 answers

Is it okay to mark a @ViewChild as private?

Is it okay to mark a ViewChild as private, if it's not being referenced in the template? I have been able to do this, then build and serve with the '--prod' flag, and encountered no errors. I'm currently using Angular 7. @ViewChild(HelloComponent)…
8
votes
2 answers

Angular2 @ViewChild ElementRef offsetHeight always 0

I'm trying to reference a component's element in my template and the height is always 0. export class LoginComponent { @ViewChild("loginForm", {read: ElementRef}) loginForm; constructor() {} ngAfterViewInit() { console.log("form…
Josh Elias
  • 3,250
  • 7
  • 42
  • 73
8
votes
2 answers

@ViewChild: access grand-child methods

I have three nested components, called level0, level1 and level2, defined like so: @Component({ selector: 'level2', template: `
I am level 2
`, }) export class Level2 { getName(){ return "my name is 'TWO'"; …
Manube
  • 5,110
  • 3
  • 35
  • 59
7
votes
1 answer

How to fix @ViewChild ElementRef undefined in NativeScript Angular?

I am developing a mobile application with NativeScript (CLI v5.2.0) Angular (v7.2.3) and I have my @ViewChild ElementRef that was undefined. I checked the presence of ViewChild and ElementRef in the import of "@angular/core", renamed my @ViewChild…
7
votes
2 answers

What type should @ViewChild variables have?

If I have a component such as this, which references a native HTML element, what type annotation should be used? @Component({ template: `
7
votes
1 answer

Unable to capture nativeElement for custom angular components

I am unable to get the reference of nativeElement of my custom Elements. I have a template like this:
Code used to…
Gary
  • 2,293
  • 2
  • 25
  • 47
6
votes
1 answer

Angular ViewChildren does not see all children from ngFor immediately

I have a strange behaviour of @ViewChildren corresponding to children components generated by ngFor. @ViewChildren query does not see element standing in array for a quite long time. All my code is in the Plunker - see with console opened. This is…
Michał Lis
  • 461
  • 1
  • 6
  • 19
5
votes
1 answer

Click button from library programatically by clicking it's outer div angular

I'm new to Angular. The Google button from the @abacritt/angularx-social-login is just an icon and I want to change its appearance by placing the icon in a div with the words "Sign in with Google" next to it. I'm able to authenticate with Google…
5
votes
3 answers

Accessing the child component residing under ng-component with ViewChild in Angular

I have parent component, there is a ng-template section. Under this ng-template section there is a Child Component. Now I would like to access this child component with ViewChild decorator. After getting with ViewChild I want to execute a function…
mnu-nasir
  • 1,642
  • 5
  • 30
  • 62
5
votes
1 answer

Why is @ViewChild still undefined even though the *ngIf is set to true

I came across the following issue which was fixed by the {static: false} property in @ViewChild. This stackoverflow Q/A helped with that How should I use the new static option for @ViewChild in Angular 8?. I wanted to understand this scenario better…
Jac Frall
  • 403
  • 7
  • 15
5
votes
2 answers

When to use @ViewChild, @Input and @Output?

When to use ViewChild, Input and Output? As one can achieve all the properties of class using ViewChild then why to use Input and Output. In simple scenario Input and Output is best. But I have 4 to 5 levels deep hierarchies of component. In that…
Ashish S
  • 638
  • 6
  • 14
5
votes
1 answer

Angular: ViewChild audio element as HTMLAudioElement?

I'm trying to get an audio element inside a component. At first I was doing it the old fashioned way: $player: HTMLAudioElement; ... ngOnInit() { this.$player = document.getElementById('stream') } But I wanted to do it The Angular Way™ so I…
Kenmore
  • 1,525
  • 3
  • 16
  • 39
5
votes
4 answers

child parent communication best practices in Angular

I'm trying to become better at Angular and I want to know the best practices between child-parent communication. My current app I want to work on is on Angular 6. I know I can communicate between child-parent components using @ViewChild, @Output or…
Patricio Vargas
  • 5,236
  • 11
  • 49
  • 100
5
votes
2 answers

Angular 5: Access element inner html from the component for dynamically generated elements

I have an angular component in my component I need to access the inner html of element. For this I am able to get this for single element using ViewChild with template ref. but in the ngFor when I generate element and template ref dynamically it did…
Khemraj
  • 109
  • 1
  • 9
5
votes
2 answers

Ionic why nativeElement of undefined?

I have an ionic application using segments, and on one segment I want to display google maps. When we load this segment first it works but when I go to another segment and I want to go back to the google maps segment, I get an error message. And I…
Kol2gaR
  • 202
  • 2
  • 13
1 2
3
29 30