Angular2 How to trigger (click) event without clicking
Solution 1
Give it a ViewChild reference :
<div #myDiv id="tutor-price" (click)="passCharge(r.value['charge'])"><span id="month">월 8회</span> <span id="price"> {{r.value['charge']}} </span></div>
In your component :
@ViewChild('myDiv') myDiv: ElementRef<HTMLElement>;
triggerFalseClick() {
let el: HTMLElement = this.myDiv.nativeElement;
el.click();
}
Solution 2
You can trigger click event in ngOnInit() like this: `
<div #divClick id="tutor-price" (click)="passCharge(r.value['charge'])">
<span id="month">월 8회</span>
<span id="price"> {{r.value['charge']}} </span>
</div>`
In component.ts file
import { Component, OnInit, AfterViewInit, ElementRef, ViewChild } from '@angular/core';
@Component({
//component decoraters
})
export class MyComponent implements OnInit, AfterViewInit {
@ViewChild('divClick') divClick: ElementRef;
ngOnInit() {
// your other code
setTimeout(() => {
this.divClick.nativeElement.click();
}, 200);
}
}
Solution 3
Expanding on the accepted answer, if you get the error "Cannot read property 'nativeElement' of undefined" or even "Cannot read property click of undefined", as OP clearly states in the comments to answer provided, use this solution:
If you want to get a reference of an element that hosts a component or directive you need to specify that you want the element instead of the component or directive
@ViewChild('myDiv', { read: ElementRef }) myDiv: ElementRef<HTMLElement>;
Related videos on Youtube
Lea
Updated on July 09, 2022Comments
-
Lea almost 2 years
I want to pass data from HTML to the component so I've created an event like this:
<div id="tutor-price" (click)="passCharge(r.value['charge'])"><span id="month">월 8회</span> <span id="price"> {{r.value['charge']}} </span></div>
And in component:
passCharge(charge){ this.charge = charge; console.log(this.charge,"give me the number") }
If I click the event, I see everything's working fine. But I want to trigger this click event automatically since I want the component to use 'this.charge' value right away once the component is done the loading.
Is there any way I can trigger (click) event automatically?
-
eko almost 7 yearsCall the method?
-
Deepa almost 7 yearsYou can call the method on ngOnInit() { } , it will be called once the page is loaded.
-
Lea almost 7 yearsI can't just call the method passCharge(r.value['charge']); in ngOnInit() casue component can't read r.value['charge'] it won't compile If I add otheriwse, it doens't print what I want
-
eko almost 7 yearsWhat is
r
in your code?
-
-
Lea almost 7 yearsWhat is HTMLElement? I tried this but it's printing error ./MainComponent class MainComponent - inline template:57:8 caused by: Cannot read property 'nativeElement' of undefined
-
Admin almost 7 yearsHTMLElement is an HTML element, what is usually returned by
document.getElementById()
in native JS. This error occurs because you didn't declare the elementRef. -
Lea almost 7 yearsBy declare u meant this? 1.import { Component, OnInit, ViewChild, ElementRef } from '@angular/core'; 2. constructor( el:ElementRef) { } I tried both still the same error :(
-
Admin almost 7 yearsDon't put it in the contructor, put it as a local variable.
-
Admin almost 7 yearsAlso, where do you call the fakeClick function ?
-
Lea almost 7 yearsi call the fakeClick function from ngOnInit
-
Admin almost 7 yearsimplement AfterViewInit and call it in it then (OnInit hasn't loaded the view yet)
-
Admin almost 7 yearsBut if you want to test if it works, simply add a timeout !
-
Harshal almost 6 yearsERROR TypeError: el.click is not a function in chrome. I get the valid HTMLElement.
-
Admin almost 6 yearsIf you have the
HTMLElement
and can't trigger aclick
, then please provide a minimal reproducible example and make a question on SOF. -
Alessandro Scarozza over 5 years@trichetriche why not just "this.myDiv.nativeElement.click();"
-
Admin over 5 years@Xan because I explain, instead of giving the quickest solution.
-
ThinkTank about 4 yearsthis.myDiv.nativeElement; is undefined ?? doesn't work even after use after AfterViewInit in angular 7
-
Travis almost 4 yearsHow would this even compile? passCharge(charge) and this.charge = charge is being done inside the class itself and not in any method?
-
Hardik Patel over 3 years@ViewChild('myDiv', {static: true}) myDiv: ElementRef;