Angular2如何在不单击的情况下触发

Angular2如何在不单击的情况下触发

本文介绍了Angular2如何在不单击的情况下触发(单击)事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据从HTML传递到组件,所以我创建了这样的事件:

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>

在组件中:

passCharge(charge){
   this.charge = charge;
   console.log(this.charge,"give me the number")
}

如果我单击该事件,则一切正常.但是我想自动触发此click事件,因为我希望组件在加载完成后立即使用'this.charge'值.

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?

推荐答案

为其提供ViewChild参考:

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>

在您的组件中:

@ViewChild('myDiv') myDiv: ElementRef<HTMLElement>;

triggerFalseClick() {
    let el: HTMLElement = this.myDiv.nativeElement;
    el.click();
}

这篇关于Angular2如何在不单击的情况下触发(单击)事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 01:49