输入中的Angular4遮罩字符而不更改其值 [英] Angular4 mask characters in Input without changing its value

查看:67
本文介绍了输入中的Angular4遮罩字符而不更改其值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在输入中屏蔽除Angular 4中的最后四个字符外的所有字符(即显示*)?

How can I mask all characters (i.e. show *) in input except last four in Angular 4 without changing the actual value?

例如:数字应在输入文本框中看起来像*** 1234,值应为7671234.

For example: number should look like ***1234 inside input textbox and the value should be 7671234.

推荐答案

如果要在TemplateDriveForm中使用指令,则必须添加AfterViewChecked事件,因为在ngOnInit中我们无法获得真实值"

If you want to use the directive in a TemplateDriveForm you must add AfterViewChecked event because in ngOnInit we cannot get the "real value"

@Directive({
  selector: '[stringHide]'
})
export class StringDirective implements OnInit,AfterViewChecked { //<--AddAfterViewChecked
private value: any; 
private initialized:boolean; //<--add a new variable
....
//Add ngAfterViewCheckecd 
ngAfterViewChecked()
{
   if (this.element.value && !this.initialized)
   {
      this.initialized=true;
      this.value = this.element.value;
      this.formatValue();
    }
 }
 .....

这篇关于输入中的Angular4遮罩字符而不更改其值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆