我想创建自己的指令,但是它不起作用:
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]
答案 0 :(得分:0)
我认为情况不正确。
if(this.format == "lowercase") {
this.el.nativeElement.value = value.toLowerCase();
} else {
this.el.nativeElement.value = value.toUpperCase();
}