0

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

1 Answer 1

1

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();
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, but it give "ERROR TypeError: this.tasks is undefined". I'll continue search for another problems that can cause it
yes that is correct behavior. The click method should be called after the view has been initialized. I have updated the answer.
it continue failing, I include ElementRef in the constructor and it continues failing
where have you defined @ViewChild('tasks') tasks:ElementRef; in your class ?
create a plunker link with your code please, or add your component class code in the question
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.