File

src/app/component/mobile-input/mobile-input.component.ts

Metadata

selector stbui-mobile-input
styleUrls ./mobile-input.component.scss
templateUrl ./mobile-input.component.html

Index

Properties
Inputs
Outputs

Constructor

constructor(elementRef: ElementRef)
Parameters :
Name Type Optional
elementRef ElementRef No

Inputs

disabled
Type : boolean
Default value : false
placeholder
Type : string
Default value : '请输入手机号码'
value
Type : string

Outputs

valueChange
Type : EventEmitter<any>

Properties

Private delay
Type : number
Default value : 300
Private regular
Type : any
Default value : /^[1][3,4,5,7,8][0-9]{9}$/
import {
  Component,
  Input,
  Output,
  EventEmitter,
  ElementRef
} from '@angular/core';
import { Observable, fromEvent } from 'rxjs';
import { map, debounceTime, distinctUntilChanged } from 'rxjs/operators';

@Component({
  selector: 'stbui-mobile-input',
  templateUrl: './mobile-input.component.html',
  styleUrls: ['./mobile-input.component.scss']
})
export class MobileInputComponent {
  private delay: number = 300;
  private regular: any = /^[1][3,4,5,7,8][0-9]{9}$/;

  @Input() placeholder: string = '请输入手机号码';
  @Input() disabled: boolean = false;
  @Input() value: string;

  @Output() valueChange: EventEmitter<any> = new EventEmitter<any>();

  constructor(private elementRef: ElementRef) {
    fromEvent(this.elementRef.nativeElement, 'keydown')
      .pipe(
        map(() => this.value),
        debounceTime(this.delay),
        distinctUntilChanged()
      )
      .subscribe(input =>
        this.valueChange.emit({
          value: input,
          isMobile: this.regular.test(input)
        })
      );
  }
}
<input type="text" maxlength="11" [placeholder]="placeholder" [(ngModel)]="value" [disabled]="disabled">

./mobile-input.component.scss

Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""