AngularMaterial的matInput格式化问题
创始人
2024-10-28 09:01:16
0

可以通过在matInput上绑定ngModel,使用Angular中的pipe或directive来进行格式化。

示例代码:

  1. 使用pipe

在HTML中:


  

在Component中:

// 定义一个数字类型的变量
myNumberValue: number = 123.456;

// 在NgModule中导入FormsModule
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';

@NgModule({
  imports: [ BrowserModule, FormsModule ],
  declarations: [ AppComponent ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

这里使用了number pipe来格式化数字,控制小数点后保留的位数。

  1. 使用directive

在HTML中:


  

在Directive中:

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

@Directive({
  selector: '[appMyFormat]'
})
export class MyFormatDirective {

  constructor(private el: ElementRef) { }

  @HostListener('blur') onBlur() {
    let val = this.el.nativeElement.value;
    val = val.toUpperCase();
    this.el.nativeElement.value = val;
  }
}

这里使用了directive来监听input的blur事件,实现了将输入的小写字母转换为大写字母的功能。

注意,在NgModule中需要声明这个Directive:

@NgModule({
  imports: [ BrowserModule, FormsModule ],
  declarations: [ AppComponent, MyFormatDirective ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

相关内容

热门资讯

最新技巧!德普之星透视,new... 最新技巧!德普之星透视,newpoker脚本,真是有挂(有挂方略)1、脚本辅助下载、免费透视脚本等服...
值得注意的是!wepoker轻... 值得注意的是!wepoker轻量版透视系统,德普之星透视辅助软件激活码,切实确实有挂(有挂讲解)1、...
2分钟细说!wepoker免费... 2分钟细说!wepoker免费钻石,wepoker怎么破解游戏,总是是真的有挂(了解有挂)1、操作简...
插件辅助挂!hhpoker脚本... 插件辅助挂!hhpoker脚本,德普之星透视辅助软件是真的吗,原来存在有挂(今日头条)1、超多福利:...
我来教教大家!hhpoker到... 我来教教大家!hhpoker到底可以作弊码,德普之星透视辅助插件,其实是真的有挂(有挂实锤)1、有没...
值得注意的是!智星德州插件,德... 值得注意的是!智星德州插件,德普辅助器怎么用,其实真的是有挂(揭秘有挂)1、德普辅助器怎么用有没有辅...
记者揭秘!hhpoker的辅助... 记者揭秘!hhpoker的辅助是真的吗,sohoo开挂辅助,竟然有挂(有挂秘诀)1、首先打开辅助器下...
为切实保障!wepoker买钻... 为切实保障!wepoker买钻石有用吗,德扑之星有辅助软件吗,都是是有挂(揭秘有挂)德扑之星有辅助软...
目前!pokerworld修改... 目前!pokerworld修改器,德普之星透视辅助,本来存在有挂(有挂教学)1、在德普之星透视辅助插...
三分钟了解!wepoker怎么... 三分钟了解!wepoker怎么设置房间,wepoker脚本下载,都是是真的有挂(有挂实锤)1、很好的...