angular - 自定义 Angular 输入掩码

标签 angular mask directive

我确实有一个输入字段,其中填充了JSON对象数据。

 dimension: {
   length: 10.00,
   width: 20.00,
   height: 30.00,
 }

输入如下所示:

 <input matInput [placeholder]="Dimension (LxHxW)" formControlName="dimensions" 
        name="dimensions" mask="00.0x00.0x00.0" [specialCharacters]="['x', '.']" 
        [clearIfNotMatch]="true" [showMaskTyped]="true"
  />

其中维度在 typescript 代码中内置为:

 let dimensions = null;
    if (dimensionObject) {
      dimensions = '' + dimensionObject.length + 'x' + dimensionObject.width + 'x'
        + dimensionObject.height;
    }

The goal is to map correctly the data on the mask and obtain the length, width and height concatenated with an x in between-> obtain a flexible mask.

当维度值的长度不同时会出现问题:

例如如果尺寸为 2.3 x 12.3 x 42.2 而不是 2.3 x 12.3 x 42.2,它将显示 23.1 x 23.4 x 22。(x 偏移)。

你们能找到任何解决方案吗?

最佳答案

我猜你必须使用模式

以下示例,取自this article on CSS-tricks, by Chris Coyier但是created by Estelle Weyl ,展示如何使用 A1A 1A1 形式处理加拿大邮政编码:

 <div>
   <label for="czc">Canadian Zip Code</label>
   <input id="czc" placeholder="XXX XXX" pattern="\w\d\w \d\w\d" class="masked" 
       data-charset="_X_ X_X" id="zipca" type="text" name="zipcodeca" 
       title="6-character alphanumeric zip code in the format of A1A 1A1" />
 </div>

在您的情况下,您应该只更改模式正则表达式。

关于angular - 自定义 Angular 输入掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54765939/

相关文章:

javascript - Angular 2 - 自定义表单控件 - 禁用

javascript - 如何将数据从 JSON API 传递到 TypeScript 模型

ios - 屏蔽 UIImageView 不起作用

python - OpenCV/Python : Mask on fftimage - Why do we need two channels?

Angular:在静态内容文件上设置 maxAge

angular - 上传图像时仅在 iOS 移动 safari 中出现虚假 CORS 失败

html - 是否可以在 SVG 文件中创建蒙版?

c - 使用 #ifndef 指令在多个 C 代码中重新定义符号

javascript - 指令 templateUrl 重定向到/

wpf - "are you missing a using directive or an assembly reference?"但命名空间和引用是正确的