首页 > 解决方案 > 如何为小写创建我自己的指令

问题描述

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

输入格式.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]

标签: angular

解决方案


我认为条件不正确。

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

推荐阅读