如何创建我自己的小写指令

时间:2019-10-25 17:43:45

标签: angular

我想创建自己的指令,但是它不起作用:

input-format.directive.ts

import { Directive, HostListener, ElementRef,Input } from '@angular/core';

@Directive({
  selector: '[appInputFormat]'
}) 
export class InputFormatDirective {
  @Input('format') format;

  constructor(private el: ElementRef) { }

  @HostListener('blur') onBlur(){
    let value:string = this.el.nativeElement.value;

    if(this.format=="lowercase"){
      this.el.nativeElement.value = value.toUpperCase();
    }else{
      this.el.nativeElement.value = value.toLowerCase();
    }

  }
}

app.component.html

[<input type="text" appInputFormat \['format'\]="'lowercase'" >][1]

1 个答案:

答案 0 :(得分:0)

我认为情况不正确。

if(this.format == "lowercase") {
    this.el.nativeElement.value = value.toLowerCase();
} else {
    this.el.nativeElement.value = value.toUpperCase();
}