text-mask: selectionStart is undefined (Ionic RC5)
Upgraded to Ionic RC5 and it appears to have broken input masking.
Cannot read property 'selectionStart' of undefined at Object.update (textMaskCore.js:1) at MaskedInputDirective.onInput (angular2TextMask.js:42)
I tried to debug this a bit and I can confirm the ‘selectionStart’ value is contained within the element but It doesn’t look like the textMaskInputElement.update() function is processing the information correctly anymore.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 12
- Comments: 24 (6 by maintainers)
Commits related to this issue
- fix(angular2): load the directive on ngAfterViewInit closes #323 — committed to ihadeed/text-mask by ihadeed 7 years ago
Same issue. Could someone resolve this?
To enable this in prod mode you also need to replace this:
"ngOnInit":[{"__symbolic":"method"}],to this:"ngAfterViewInit":[{"__symbolic":"method"}],in node_modules/angular2-text-mask/dist/angular2TextMask.metadata.json at 1:902 Have a nice day! )You need to replace this line:
MaskedInputDirective.prototype.ngOnInit = function () {to this:MaskedInputDirective.prototype.ngAfterViewInit = function () {in node_modules/angular2-text-mask/dist/angular2TextMask.js at line 22@ihadeed looking at the debugger, I can confirm that the element does indeed not exist yet.
I would suggest that you try ngAfterContentInit or ngAfterViewInit instead of ngOnInit.
See: lifecycle-hooks
@AfonsoHenrique use my solution or try to make npm clear cache BEFORE npm install
I updated the package json of my project to angular2-text-mask@4.0.0 and ran npm install. The selectionStart error stopped, but the mask don’t appear anymore on the browser (Chrome) or the device (iOS), don’t log anything either.
<ion-input placeholder=“Seu CPF” [textMask]=“{mask: masks.cpf}” [formControl]=“controlFormRegistro.controls[‘cpf’]” type=“tel”></ion-input>
this.masks = { date: [/\d/, /\d/, ‘/’, /\d/, /\d/, ‘/’, /\d/, /\d/, /\d/, /\d/], cpf: [/\d/, /\d/, /\d/, ‘.’, /\d/, /\d/, /\d/, ‘.’, /\d/, /\d/, /\d/, ‘-’, /\d/, /\d/], };
I can confirm this. Here’s a Plunkr with the issue: http://plnkr.co/edit/g1gQMOs8ZzoP4ISh51Il?p=preview
The mask doesn’t show at all. It seems like there is an issue initializing the directive. The error message indicates that the value of
inputElementis undefined, which is why it can’t read the value ofinputElement.selectionStart(see https://github.com/text-mask/text-mask/blob/17ad73bd74bb574d09d2554d18e535a364084e07/core/src/createTextMaskInputElement.js#L62).My best guess is that the
inputelement doesn’t exist when thetextMaskdirective runs. So when ourngOnInitruns, theinputelement doesn’t exist in the DOM yet, which leads toinputElementbeingundefined.We can do more tests to make sure this is the issue. If it is, we can apply a fix like this to make sure that the mask will always work: