Ngx-mask: Using setAttribute to dynamically set mask on input not working

Created on 7 Jun 2019  路  6Comments  路  Source: JsDaddy/ngx-mask

I am trying to set a mask on an html element dynamically using the setAttribute method.

The mask, ex. "0000-000-000-SS" is not being enforced by the input when set dynamically.

The html element appears to be created correctly, with the mask attribute present, but there is no "ng-reflect-mask-expression" created in the html input element when dirtied. However when setting the mask attribute directly from the component html the attribute is added to the input upon being dirtied.

This is the dirtied-non-dynamic input. As you see there is an attribute for ng-reflect-mask-expression, indicating that the module is working correctly.

Dirtied-non-dynamic-masked-input

This is the dirtied-dynamic input. There is no attribute for ng-reflect-mask-expression, indicating an error with the mask enforcement.

Dirtied-dynamic-masked-input

Has anyone experienced this issue, and if so, is there a solution?

I am using Angular 7, and the latest version of ngx-mask.

Most helpful comment

The workaround seems to be dynamically generating components using a component factory with static html and property bindings, rather than dynamically generating the DOM elements themselves. This is because the directives allowing the Javascript behind the DOM element to work(and set the mask), are created at compile time and simply adding the element to the DOM any other time in the lifecycle of the app will not allow the directives to function properly. My team and I also switched to a different Masked Text Box: the Kendo UI masked text box by Telerik. Hope this helped.

All 6 comments

Hi @patrickbaciu . Thanks

In fact, once we start data binding, we are no longer working with HTML attributes. We aren't setting attributes. We are setting the properties of DOM elements, components, and directives.

Also angular don't sync property in this way

Solution for you use binding syntax

<input [mask]="myProp" />

in component where you need 
.... 
 this.mask = 'new mask' 
.... 

@NepipenkoIgor I don't think you understand my issue. We are not simply changing the mask property of an already existing DOM element. I am creating a DOM from within my component, so binding syntax is not an option. Ideally I would like to do something like this:

var maskedTextElement = document.createElement('input'); maskedTextElement.setAttribute('mask', "0000-000-000-SS");

Look at #365, looks like the same issue is not resolved.

Why is this closed?

The workaround seems to be dynamically generating components using a component factory with static html and property bindings, rather than dynamically generating the DOM elements themselves. This is because the directives allowing the Javascript behind the DOM element to work(and set the mask), are created at compile time and simply adding the element to the DOM any other time in the lifecycle of the app will not allow the directives to function properly. My team and I also switched to a different Masked Text Box: the Kendo UI masked text box by Telerik. Hope this helped.

@NepipenkoIgor Could you confirm that you can use [mask] on input?

Can't bind to 'mask' since it isn't a known property of 'input'. ("
           [ERROR ->][mask]="masker"
           >

@NepipenkoIgor Could you confirm that you can use [mask] on input?

Can't bind to 'mask' since it isn't a known property of 'input'. ("
           [ERROR ->][mask]="masker"
           >

Try adding NgxMaskModule.forChild() to your child module.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lramondev picture lramondev  路  3Comments

v1d3rm3 picture v1d3rm3  路  4Comments

renanBritz picture renanBritz  路  3Comments

sbeka picture sbeka  路  3Comments

dissy123 picture dissy123  路  3Comments