1

I am very new to Angular 2 the question is How to trigger click on an HTML file element from a component?

  rankingFilter() {

    this.RepsloaderShow = true;
    this.reps = [];
    var data = {
        from: this.fromFilter,
        to: this.toFilter,
        user_search: this.user_search
    }
    this.http.post(CONSTANTS.baseApiUrl + 'reports/rankings_individuals', data)
        .map(res => res.json())
        .subscribe((data) => {
            this.reps = data;
            this.RepsloaderShow = false;
            // Trigger click here

        });
}

HTML File (click event on class='rep')

    <ul class="carousel-indicators section-nav">
        <li class=" icon_0 icon active rep" data-target="#slideable" data-slide-to="0">
            <span class='shh'>Reps</span>
            <p style="display: none;">Reps</p>
        </li>
        <li class="icon_0 icon team" data-target="#slideable" data-slide-to="1">
            <span class='shh'>Teams</span>
            <p style="display: none;">Teams</p>
        </li>
        <li class="icon_0 icon award" data-target="#slideable" data-slide-to="2">
            <span class='shh'>Class Ranks</span>
            <p style="display: none;">Class Ranks</p>
        </li>
    </ul>
Amy
  • 59
  • 2
  • 5
  • From which element, and for what purpose? One example is shown in http://stackoverflow.com/questions/36342890/in-angular2-how-to-know-when-any-form-input-field-lost-focus/36348311#36348311 – Günter Zöchbauer Oct 18 '16 at 08:59

1 Answers1

6

For dispatching from the host element

constructor(private elRef:ElementRef, private renderer:Renderer) {}

For dispatching from an element inside the template

<some-elem #target></some-element>
@ViewChild('target') elRef:ElementRef;

And then dispatch the event:

  ...
    this.renderer.invokeElementMethod(this.elRef.nativeElement, 
        'dispatchEvent', 
        [new MouseEvent('click', { bubbles: true, cancelable: true })]);
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567