输入中的Angular4掩码字符,不更改其值
如何在不更改实际值的情况下屏蔽输入中除Angular 4中最后四个字符以外的所有字符(即show*) 例如:输入文本框内的数字应类似***1234,值应为7671234。使用指令输入中的Angular4掩码字符,不更改其值,angular,Angular,如何在不更改实际值的情况下屏蔽输入中除Angular 4中最后四个字符以外的所有字符(即show*) 例如:输入文本框内的数字应类似***1234,值应为7671234。使用指令 @Directive({ selector: '[stringHide]' }) export class StringDirective implements OnInit { private value: any; //the variable where we save the "true value"
@Directive({
selector: '[stringHide]'
})
export class StringDirective implements OnInit {
private value: any; //the variable where we save the "true value"
private element: HTMLInputElement
constructor(private el: ElementRef, private form: ControlContainer) {
this.element = el.nativeElement;
}
ngOnInit() { //It's necesary use OnInit, otherwise the first time not formtted
this.value = this.element.value;
this.formatValue();
}
@HostListener('input') onChange() { //when a change happens save the value in a variable
this.value = this.element.value;
}
@HostListener('blur') onBlur() { //when lost the focus call format function
this.formatValue();
}
@HostListener('focus') onFocus() { //when get the focus recover the true value
this.element.value = this.value;
}
formatValue() { //here, change the apperance of the input
//I choose that if the value.length>14 only show 10 "*"
let len=this.element.value.length;
this.element.value = (len <= 4) ? this.element.value:
(len>14)? "**********"+this.element.value.substr(len - 4):
"**************".substring(0,len-4)+this.element.value.substr(len - 4);
}
}
@指令({
选择器:“[stringHide]”
})
导出类StringDirective实现OnInit{
private value:any;//保存“真值”的变量
私有元素:HTMLInputElement
构造函数(私有el:ElementRef,私有表单:ControlContainer){
this.element=el.nativeElement;
}
ngOnInit(){//必须在它上面使用,否则第一次就不会形成
this.value=this.element.value;
这个.formatValue();
}
@HostListener('input')onChange(){//发生更改时,将值保存在变量中
this.value=this.element.value;
}
@HostListener('blur')onBlur(){//丢失焦点调用格式函数时
这个.formatValue();
}
@HostListener('focus')onFocus(){//获取焦点时恢复真实值
this.element.value=this.value;
}
formatValue(){//在这里,更改输入的外观
//如果值.length>14仅显示10“*,则我选择该值
设len=this.element.value.length;
this.element.value=(len 14)?“********”+this.element.value.substr(len-4):
“**********”.substring(0,len-4)+this.element.value.substr(len-4);
}
}
使用指令
@Directive({
selector: '[stringHide]'
})
export class StringDirective implements OnInit {
private value: any; //the variable where we save the "true value"
private element: HTMLInputElement
constructor(private el: ElementRef, private form: ControlContainer) {
this.element = el.nativeElement;
}
ngOnInit() { //It's necesary use OnInit, otherwise the first time not formtted
this.value = this.element.value;
this.formatValue();
}
@HostListener('input') onChange() { //when a change happens save the value in a variable
this.value = this.element.value;
}
@HostListener('blur') onBlur() { //when lost the focus call format function
this.formatValue();
}
@HostListener('focus') onFocus() { //when get the focus recover the true value
this.element.value = this.value;
}
formatValue() { //here, change the apperance of the input
//I choose that if the value.length>14 only show 10 "*"
let len=this.element.value.length;
this.element.value = (len <= 4) ? this.element.value:
(len>14)? "**********"+this.element.value.substr(len - 4):
"**************".substring(0,len-4)+this.element.value.substr(len - 4);
}
}
@指令({
选择器:“[stringHide]”
})
导出类StringDirective实现OnInit{
private value:any;//保存“真值”的变量
私有元素:HTMLInputElement
构造函数(私有el:ElementRef,私有表单:ControlContainer){
this.element=el.nativeElement;
}
ngOnInit(){//必须在它上面使用,否则第一次就不会形成
this.value=this.element.value;
这个.formatValue();
}
@HostListener('input')onChange(){//发生更改时,将值保存在变量中
this.value=this.element.value;
}
@HostListener('blur')onBlur(){//丢失焦点调用格式函数时
这个.formatValue();
}
@HostListener('focus')onFocus(){//获取焦点时恢复真实值
this.element.value=this.value;
}
formatValue(){//在这里,更改输入的外观
//如果值.length>14仅显示10“*,则我选择该值
设len=this.element.value.length;
this.element.value=(len 14)?“********”+this.element.value.substr(len-4):
“**********”.substring(0,len-4)+this.element.value.substr(len-4);
}
}
如果要在TemplateDriveForm中使用该指令,必须添加AfterViewChecked事件,因为在ngOnInit中我们无法获得“真实值”
@指令({
选择器:“[stringHide]”
})
导出类StringDirective实现OnInit,AfterViewChecked{//如果要在TemplateDriveForm中使用该指令,则必须添加AfterViewChecked事件,因为在ngOnInit中无法获取“实际值”
@指令({
选择器:“[stringHide]”
})
导出类StringDirective实现OnInit,AfterViewChecked{//如果在输入框中移动光标,该指令是否有效?该指令在反应式表单中工作正常。当该指令与模板驱动表单一起使用时出现问题。如果解决问题,我将更改代码。如果在输入框中移动光标,该指令是否有效?该指令在反应式表单中工作正常。当该指令与模板驱动表单一起使用时出现问题ective它与模板驱动的表单一起使用。如果我要解决,我将更改代码