Ruben Conde Morales
Ruben Conde Morales

Reputation: 3

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

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

I have tryed document.getElementById("tasks").click() and ngAfterViewInit()

Upvotes: 0

Views: 2428

Answers (1)

FAISAL
FAISAL

Reputation: 34673

To click the element, you can do the following:

Change your html to following:

<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();
}

Upvotes: 1

Related Questions