zoukankan      html  css  js  c++  java
  • angular keydown 例子

    ...

     https://angular.cn/guide/observables keydown example

    import { Component } from '@angular/core';
    import { Observable, fromEvent } from 'rxjs';
    
    @Component({
      selector: 'app-cust',
      template: `<p><button type="button" (click)="runExample()">{{title}}</button><input id="name" placeholder="type here"></p>`,
    })
    export class CustomersComponent {
    
      title = 'fromEvent - How to capture an event e.g. a keypress';
    
      runExample() {
        console.clear();
    
        // const myInput = document.getElementById('example-d');
        // const myObserverable = fromEvent(myInput, 'keyup');
        //
        // myObserverable
        //   .subscribe(
        //     value => console.log(value),
        //     err => console.error(err),
        //     () => console.log('Streaming is over')
        //   );
    
        function fromEvent(target, eventName) {
          return new Observable((observer) => {
            const handler = (e) => observer.next(e);
    
            // Add the event handler to the target
            target.addEventListener(eventName, handler);
    
            return () => {
              // Detach the event handler from the target
              target.removeEventListener(eventName, handler);
            };
          });
        }
    
        const ESC_KEY = 27;
        const nameInput = document.getElementById('name') as HTMLInputElement;
    
        const subscription = fromEvent(nameInput, 'keydown').subscribe((e: KeyboardEvent) => {
          if (e.keyCode === ESC_KEY) {
            nameInput.value = '';
          }
        });
    
    
      }
    }
    

      

  • 相关阅读:
    关于正则表达式
    hashilib模块和hmac模块
    PyYAML模块和ConfigParser模块
    xml处理模块
    shutil模块(文件,文件夹,压缩包处理)
    十四、浏览器检测
    十三、BOM
    十二、匿名函数和闭包
    并发,并行,同步,异步的区别
    java中常见的类,接口,包,异常
  • 原文地址:https://www.cnblogs.com/eiguleo/p/14587691.html
Copyright © 2011-2022 走看看