angular - 如何为小写创建我自己的指令
问题描述
我想创建自己的指令,但它不起作用:
输入格式.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]
解决方案
我认为条件不正确。
if(this.format == "lowercase") {
this.el.nativeElement.value = value.toLowerCase();
} else {
this.el.nativeElement.value = value.toUpperCase();
}
推荐阅读
- c++ - 从文件中删除记录
- sql - 如何使用函数从 SQL Server 中的名称字符串中删除多个可能的前缀
- mysql - 用于连接具有相同值的行的 SQL 查询
- batch-file - 将文件名附加到文件夹名称并替换 `-` 并删除空格
- facebook - 如何使用我的访问令牌访问 Facebook 页面的公共元数据?
- ffmpeg - 我错过了 FFMPEG 中的超时参数吗?
- google-maps - Ontap 在颤动中无法使用 Google 地图标记
- c# - Unity将类的某些部分序列化为json
- c - 在子进程中读取文件的内容并使用 C 中的管道将其传递给父进程
- javascript - 如何使用 Leaflet Control Geocode?