Angular2 How to trigger (click) event without clicking

后端 未结 3 535
耶瑟儿~
耶瑟儿~ 2020-12-25 12:12

I want to pass data from HTML to the component so I\'ve created an event like this:

相关标签:
3条回答
  • 2020-12-25 12:38

    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>
    

    In your component :

    @ViewChild('myDiv') myDiv: ElementRef<HTMLElement>;
    
    triggerFalseClick() {
        let el: HTMLElement = this.myDiv.nativeElement;
        el.click();
    }
    
    0 讨论(0)
  • 2020-12-25 12:48
    <div #tutor id="tutor-price" (click)="passCharge(tutor.value['charge'])"><span id="month">월 8회</span> <span id="price"> {{tutor.value['charge']}} </span></div>
    

    Hope so this can help you ..

    import { Component, OnInit } from '@angular/core';
    tutor:any;
    @Component({
          //your component decorater tags
    })
    export class MyComponent implements OnInit{
       ngOnInit(){
          this.charge = this.tutor.value['charge'];
       }
       passCharge(charge){
       this.charge = charge;
    
    }
    
    0 讨论(0)
  • 2020-12-25 12:50

    You can trigger click event in ngOnInit() like this: `

    <div #divClick id="tutor-price" (click)="passCharge(r.value['charge'])">
        <span id="month">월 8회</span> 
        <span id="price"> {{r.value['charge']}} </span>
    </div>`
    

    In component.ts file

    import { Component, OnInit, AfterViewInit, ElementRef, ViewChild } from '@angular/core';
    @Component({
      //component decoraters
    })
    export class MyComponent implements OnInit, AfterViewInit {
      @ViewChild('divClick') divClick: ElementRef;
      ngOnInit() {
          // your other code
        setTimeout(() => {
        this.divClick.nativeElement.click();
        }, 200);
      }
    }
    
    0 讨论(0)
提交回复
热议问题