简体   繁体   English

如何激活dom元素中的点击事件(Angular 2)

[英]how to activate click event in a dom element (Angular 2)

I need to load a component at loading 我需要在加载时加载组件

<app-main id="tasks" [(ngModel)]="tasks"></app-main>

And the call from js 还有来自js的电话

public tasks;
ngOnInit() {
    this.tasks.click();
}

I have tryed document.getElementById("tasks").click() and ngAfterViewInit() 我已经尝试了document.getElementById("tasks").click()ngAfterViewInit()

To click the element, you can do the following: 要单击元素,可以执行以下操作:

Change your html to following: 将您的html更改为以下内容:

<app-main id="tasks" #tasks></app-main>

... then in your component class: ...然后在您的组件类中:

import { Component, ElementRef, ViewChild, AfterViewInit } from '@angular/core';
//...
@ViewChild('tasks') tasks:ElementRef;

ngAfterViewInit () {
    // Fire the click event of tasks
    this.tasks.nativeElement.click();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM