简体   繁体   English

如何将 fromEvent (Angular rxjs) 与多个元素一起使用?

[英]How can i use fromEvent (Angular rxjs) with multiple Elements?

I have an angular Application with multiple Buttons which have specific IDs.我有一个带有多个具有特定 ID 的按钮的角度应用程序。 each of them has a click function and I want to track if a user clicked the Button without changing anything in each of the Methods.他们每个人都有一个点击功能,我想跟踪用户是否点击了按钮而不改变每个方法中的任何内容。

example code:示例代码:

  import { Component, ViewChild, ElementRef } from '@angular/core';
import { Observable } from 'rxjs';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  
  
  

    @ViewChild('BUTTON_EDIT', {read: ElementRef}) BUTTON_EDIT;
      @ViewChild('BUTTON_ADD', {read: ElementRef}) BUTTON_ADD;
      ele: Element;
      
      ngAfterViewInit(): void {
        let clickStream;
    
        //check which element was pressed and get the id
        //how can i check it?

        clickStream = fromEvent(this.ele, 'clicked');
        var id = this.ele.id;
    
        clickStream.subscribe(event => {
          //send info
        });
      }
    }

In order to listen to multiple events you can usemerge , which为了侦听多个事件,您可以使用merge ,它

Turn multiple observables into a single observable将多个 observable 变成单个 observable

ngAfterViewInit(): void {
    merge(
        fromEvent(this.BUTTON_EDIT.nativeElement, 'click'),
        fromEvent(this.BUTTON_ADD.nativeElement, 'click'),
    ).subscribe((event: Event)=> {
      console.log(event.target.id);
    });
  }

There is also an article - Create Observable from Event using FromEvent in Angular which explains details.还有一篇文章 - Create Observable from Event using FromEvent in Angular解释了细节。

Events in Web APIs Web API 中的事件

For emitted Event there is target property, from where you can get element's id, there is a fiddle https://jsfiddle.net/a907jf5g/对于发出的事件目标属性,从那里你可以得到元素的 id,有一个小提琴https://jsfiddle.net/a907jf5g/

Angular approach to listen to click event监听点击事件的角度方法

@Component({
  template: `
    <button (click)="add()">Add</button>
    <button (click)="edit()">Edit</button>
  `,
})
export class MyComponent {
  add() {
    // add clicked
  }

  edit() {
    // edit clicked
  }
}

Angular allows you to bind more than one method to a click event. Angular 允许您将多个方法绑定到一个点击事件。 This means you can bind a common "click" method that takes in the element's ID and does not modify the event handler method that is specific to that button as follows:这意味着您可以绑定一个通用的“click”方法,该方法接受元素的 ID 并且不修改特定于该按钮的事件处理程序方法,如下所示:

<button id="add-btn" #addBtn (click)="click(addBtn.id); add()">
  Add
</button>

<button id="edit-btn" #editBtn (click)="click(editBtn.id); edit()">
  Edit
</button>

The click method does the tracking of all button clicks while the add and edit method are the event handlers that are specific to each button. click方法跟踪所有按钮的点击,而addedit方法是特定于每个按钮的事件处理程序。

StackBlitz Example StackBlitz 示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM