简体   繁体   English

如何在 Angular 6 中使用 rxjs fromEvent 定位 HTML 元素

[英]How to target an HTML element using rxjs fromEvent in Angular 6

Issue问题

I have used ngrx fromEvent operator to create an Observable from 2 input text fields, I have used document as target which is fine, but now I want to target only one input field.我使用 ngrx fromEvent 运算符从 2 个输入文本字段创建了一个 Observable,我使用文档作为目标,这很好,但现在我只想定位一个输入字段。 I am not sure sure what to use instead of document to target only one input field.我不确定使用什么代替文档来仅定位一个输入字段。

What I have done so far to get the target到目前为止我为达到目标所做的一切

  • Used document.getElementByID('someID')使用 document.getElementByID('someID')
  • Used ElementRef使用的 ElementRef
  • Used document.querySelector('someID')使用 document.querySelector('someID')

Code代码

StackBlits live editor StackBlits 实时编辑器

import { Component } from '@angular/core';
import { fromEvent } from 'rxjs';

@Component({
  selector: 'my-app',
  template: `<input type="text">
             <input type="text">`
})
export class AppComponent {
  ngOnInit() {
    fromEvent(document, 'keyup')
      .subscribe(res => console.log(res.target.value));
  }
}

Thanks for your help in advance.提前感谢您的帮助。

You can give the input field that you want to observe, a template variable.您可以为要观察的input字段提供模板变量。

You can use then use @ViewChild to get access to that input .您可以使用然后使用@ViewChild来访问该input And then use the nativeElement property on it.然后在其上使用nativeElement属性。

Now the nativeElement property will only be accessible after the view has initialized.现在,只有在视图初始化后才能访问nativeElement属性。 So you can use the AfterViewInit Component Lifecycle Hook in order to access it.因此,您可以使用AfterViewInit组件生命周期挂钩来访问它。

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

@Component({
  selector: 'my-app',
  template: `<input #toTarget type="text">
             <input type="text">`
})
export class AppComponent {

  @ViewChild('toTarget') toTarget: ElementRef;

  ngAfterViewInit() {
    fromEvent(this.toTarget.nativeElement, 'keyup')
      .subscribe(res => console.log(res.target.value));
  }
}

Here's an Updated StackBlitz for your ref.这是供您参考的更新的 StackBlitz

If you're reading this with Angular 8+, the correct way to reference @ViewChild-elements while being able to use them in ngOnInit is this:如果您正在使用 Angular 8+ 阅读本文,则在能够在 ngOnInit 中使用它们的同时引用 @ViewChild-elements 的正确方法是:

import { Component, ViewChild, ElementRef, OnInit, OnDestroy } from '@angular/core';
import { fromEvent } from 'rxjs';

@Component({
  selector: 'my-app',
  template: `
    <input #yourTarget type="text">
    <input type="text">
  `
})
export class AppComponent implements OnInit, OnDestroy {
  @ViewChild('yourTarget', {static: true}) yourTarget: ElementRef;

  subscriptions = new Subscription();

  ngOnInit(): void {
    subscriptions.add(
      fromEvent(this.yourTarget.nativeElement, 'keyup')
        .subscribe(res => console.log(res.target.value))
    )
  }

  ngOnDestroy(): void {
    subscriptions.unsubscribe();
  }
}

Notice the {static: true} inside the @ViewChild declaration: It causes Angular to know the referenced element already in the lifeCycle "OnInit".注意@ViewChild 声明中的{static: true} :它使 Angular 知道生命周期“OnInit”中已经存在的引用元素。

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

相关问题 Angular - 如何防止来自事件的 RxJs 中的 XSS 攻击? - Angular - how to prevent XSS attacks in RxJs fromEvent? 使用 FromEvent RxJS 和 Angular 订阅输入标签值更改时如何避免使用“任何”? - How to avoid using 'any' when using FromEvent RxJS with Angular to subscribe to input tag value changes? fromEvent rxjs Angular 5 工作不正常 - fromEvent rxjs Angular 5 is not properly working 如何将merge和fromEvent运算符导入Angular 6,rxjs 6.2.0? - how to import the merge and fromEvent operators to Angular 6, rxjs 6.2.0? 如何将 fromEvent (Angular rxjs) 与多个元素一起使用? - How can i use fromEvent (Angular rxjs) with multiple Elements? 我如何在 Angular 10 应用程序中等待 rxjs/fromEvent() 的结果 - How can I wait for the result of rxjs/fromEvent() in an Angular 10 app 使用主题取消订阅事件 RxJs - Unsubscribe fromEvent RxJs using Subject 无效的事件目标 - 将 fromEvent 与 Angular Material Button 一起使用时 - Invalid event target - when using fromEvent with Angular Material Button Angular - 测试 / mocking 订阅使用 rxjs fromEvent - Angular - Testing / mocking subscriptions that use rxjs fromEvent 哪种做法最适合处理 Angular 中的 HTML 事件? Angular 事件绑定还是 RxJS fromEvent function? - Which practice is best for handling HTML Events in Angular? Angular event binding or RxJS fromEvent function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM