Angular2 get clicked element id

前端 未结 9 1915
深忆病人
深忆病人 2020-11-29 20:07

I have such click event

 
 
9条回答
  •  自闭症患者
    2020-11-29 20:44

    There is no need to pass the entire event (unless you need other aspects of the event than you have stated). In fact, it is not recommended. You can pass the element reference with just a little modification.

    import {Component} from 'angular2/core';
    
    @Component({
      selector: 'my-app',
      template: `
        
        
      `
    })
    export class AppComponent {
      buttonValue: string;
    
      toggle(button) {
        this.buttonValue = button.id;
      }
    
    }
    

    StackBlitz demo

    Technically, you don't need to find the button that was clicked, because you have passed the actual element.

    Angular guidance

提交回复
热议问题