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 { }

相关内容

热门资讯

透视真的!wepoker透视底... 透视真的!wepoker透视底牌脚本,红龙poker透视,技巧知识分享(有挂总结)1、下载好wepo...
透视教程!wpk透视脚本链接,... WePoke高级策略深度解析‌;透视教程!wpk透视脚本链接,wepoker有没有插件,高科技教程(...
透视了解!wpk有脚本吗,wp... 1、透视了解!wpk有脚本吗,wpk透视工作室,科普攻略(有挂介绍)。2、wpk透视工作室透视辅助简...
透视脚本!wpk透视辅助方法,... 相信很多朋友都在电脑上玩过wpk透视辅助方法吧,但是很多朋友都在抱怨用电脑玩起来不方便。为此小编给大...
透视透视!智星德州菠萝透视插件... 透视透视!智星德州菠萝透视插件工具,德普之星辅助功能如何设置,查到实测辅助(有挂规律)1、每个玩家都...
透视美元局!hhpoker万能... 1、透视美元局!hhpoker万能辅助器,wepoker私人局辅助挂,详细教程(原本真的是有挂)2、...
透视辅助!hhpoker免费透... 一、hhpoker免费透视脚本简介了解软件请加微:136704302hhpoker免费透视脚本是一款...
透视黑科技!aapoker辅助... WePoker透视辅助工具核心要点解析‌,透视黑科技!aapoker辅助器怎么用,hhpoker是真...
透视ai代打!aapoker辅... 透视ai代打!aapoker辅助器是真的吗,wepoker脚本下载,安装程序教程(有挂方法)1、实时...
透视神器!wepoker透视版... 透视神器!wepoker透视版下载,拱趴大菠萝万能辅助器,新版2025教程(确实是有挂);值得一提的...