HTML5 event handling(onfocus and onfocusout) using angular 2

前端 未结 5 1619
逝去的感伤
逝去的感伤 2020-12-02 13:46

I have a date field and I want to remove the place holder by default.

I am using javascript onfocus and onfocusout events for removing pl

相关标签:
5条回答
  • 2020-12-02 14:21

    I've created a little directive that bind with the tabindex attribute. It adds/removes the has-focus class dynamically.

    @Directive({
        selector: "[tabindex]"
    })
    export class TabindexDirective {
        constructor(private elementHost: ElementRef) {}
    
        @HostListener("focus")
        setInputFocus(): void {
            this.elementHost.nativeElement.classList.add("has-focus");
        }
    
        @HostListener("blur")
        setInputFocusOut(): void {
            this.elementHost.nativeElement.classList.remove("has-focus");
        }
    }
    
    0 讨论(0)
  • 2020-12-02 14:34

    If you want to catch the focus event dynamiclly on every input on your component :

    import { AfterViewInit, Component, ElementRef } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent implements AfterViewInit {
    
      constructor(private el: ElementRef) {
      }
    
      ngAfterViewInit() {
           // document.getElementsByTagName('input') : to gell all Docuement imputs
           const inputList = [].slice.call((<HTMLElement>this.el.nativeElement).getElementsByTagName('input'));
            inputList.forEach((input: HTMLElement) => {
                input.addEventListener('focus', () => {
                    input.setAttribute('placeholder', 'focused');
                });
                input.addEventListener('blur', () => {
                    input.removeAttribute('placeholder');
                });
            });
        }
    }
    

    Checkout the full code here : https://stackblitz.com/edit/angular-93jdir

    0 讨论(0)
  • 2020-12-02 14:34

    The solution is this:

      <input (click)="focusOut()" type="text" matInput [formControl]="inputControl"
      [matAutocomplete]="auto">
       <mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn" >
         <mat-option (onSelectionChange)="submitValue($event)" *ngFor="let option of 
          options | async" [value]="option">
          {{option.name | translate}}
         </mat-option>
      </mat-autocomplete>
    
    TS
    focusOut() {
    this.inputControl.disable();
    this.inputControl.enable();
    }
    
    0 讨论(0)
  • 2020-12-02 14:36

    Try to use (focus) and (focusout) instead of onfocus and onfocusout

    like this : -

    <input name="date" type="text" (focus)="focusFunction()" (focusout)="focusOutFunction()">
    

    also you can use like this :-

    some people prefer the on- prefix alternative, known as the canonical form:

    <input name="date" type="text" on-focus="focusFunction()" on-focusout="focusOutFunction()">
    

    Know more about event binding see here.

    you have to use HostListner for your use case

    Angular will invoke the decorated method when the host element emits the specified event.@HostListener is a decorator for the callback/event handler method

    See my Update working Plunker.

    Working Example Working Stackblitz

    Update

    Some other events can be used in angular -

    (focus)="myMethod()"
    (blur)="myMethod()" 
    (submit)="myMethod()"  
    (scroll)="myMethod()"
    
    0 讨论(0)
  • 2020-12-02 14:46
    <input name="date" type="text" (focus)="focusFunction()" (focusout)="focusOutFunction()">
    

    works for me from Pardeep Jain

    0 讨论(0)
提交回复
热议问题