Why isn't this directive registering changes on ion-input in Angular and Ionic?

I am attempting to get this directive to function with ion input, however, I am facing two challenges that I am unsure how to resolve:

1 - Difficulty loading the mask when the ion-input is displayed - The issue is that the input element only appears as a childNode of nativeElement after some delay, making it difficult to find a suitable hook to implement. In an attempt to resolve this, I have included some code within a setTimeout function to locate the wrapped input:

public ngOnInit():void {
    setTimeout(()=>{
        this._element=this.findElement();
        resolvedPromise.then(() => this._applyValueChanges());
    },1000);
}
findElement():any{
    if (this._elementRef.nativeElement.tagName === 'INPUT') {
        return this._elementRef.nativeElement
    } else {
        let index;
        for(index=0;index<this._elementRef.nativeElement.childNodes.length;index++)
            if (this._elementRef.nativeElement.childNodes[index].tagName === 'INPUT')
                return this._elementRef.nativeElement.childNodes[index];
    }
}

2 - Another issue is that the registerOnChange method is not being called, preventing me from updating the model to remove special characters:

public registerOnChange(fn:any):void {
    this._onChange = fn;
    return;
}

Therefore, my inquiry is: how can I ensure that registerOnChange functions properly with the ion-input element? Is there a method to locate the wrapped input during onInit without relying on a setTimeout?

Answer №1

  1. The solution to the first question is to utilize the ionViewDidLoad() {} function instead of your own function.
  2. The on blur event is no longer supported in the latest version of Ionic, so you can use this alternative method instead.

    It should work without any issues.

This approach worked perfectly for me, hopefully it will work for you too.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Combining the redux toolkit function createAsyncThunk with Angular's HttpClient by leveraging the ApiService

Currently, I am working on incorporating @reduxjs/toolkit into an Angular project. Is there a way to pass an Angular service as a parameter to the callback of createAsyncThunk instead of utilizing fetch directly? I referenced the documentation for an exa ...

The ionChange functionality is not functioning as expected on ion-select elements

Is there a way to have a codeblock executed each time the value of my ion-select changes? The documentation mentions an ionChange event for this purpose. However, when attempting to use it, the event does not trigger. I experimented with the ionFocus ...

How to Use an Array to Filter Another Array in AngularJS

After attempting to filter a group of checkboxes using an array, the outcome was not what I expected: <ion-checkbox ng-repeat="user in users | filter: {id: group.members}" ng-model="user.checked">{{user.info.name}}</ion-checkbox> The specific ...

Using typescript, we can pass a generic class as a parameter to a function

Currently, I am faced with the challenge of passing a class reference as a function parameter, and then calling a static method on this particular class. Additionally, there is a possibility that in the future, I may need to instantiate the class and inclu ...

Location of images in Tomcat for an Angular 7 application

I'm facing an issue with image source paths while working on my local Windows machine (Windows 10, Visual Studio Code 1.30.2) and deploying to Tomcat 9 on an Ubuntu 18.04 server hosted on AWS. When I build on Windows using Powershell: ng build --prod ...

Certain Material-UI components appear to lack proper styling

I found a tutorial on how to incorporate material UI into my app at this link: https://mui.com/material-ui/getting-started However, I noticed that some components are not styled as expected and customizing the theme seems to have no effect... This is how ...

Declaring a sophisticated array as a property within another property in Typescript

As a newcomer to Angular and Typescript, I am facing a challenge while declaring a property with a complex array as one of its values. Here is what I have attempted: groupedItem: { customGroupId: string, cgName: string, category: [{ cu ...

There was an error in Angular at core.js:6150 stating that the object is not iterable due to a

I am facing an issue in displaying the First Name of a user in an HTML file getFirstName(id: any){ this.users = this.afs.collection('users', ref => ref.where("uid", "==", id)).valueChanges(); this.users.subscribe(users => { ...

Cultural adaptation in Angular

I have successfully created an Angular project with internationalization capabilities. However, I am now looking to implement it by culture, such as es-AR or en-NZ. Below are my configurations for Spanish and English in the angular.js file: "build": { ...

Performance problems with Ionic ion-slides due to excessive method calls

Encountering an issue with the ion-slides component where a method within my ion-slide is being called excessively, causing significant slowdown. Surprisingly, the method is triggered 900 times initially (despite showing only 100 slides) and each slight sw ...

Encountering trouble installing Angular CLI on MacOS High Sierra version 10.13.2

I am encountering an issue while trying to install Angular CLI. I have successfully installed the latest NodeJs version 8.9.4 and npm version 5.6.0. However, when I attempt to run the command npm install -g @angular/cli, I receive the following error messa ...

Encountering issues while trying to npm install globalization with Ionic version 2.2.0, facing unmet dependencies

My attempt to integrate the "globalization" plugin and access .getPreferredLanguage() in my ionic 2 app has resulted in an UNMET DEPENDENCY error. $ ionic plugin add cordova-plugin-globalization --savePlugin "cordova-plugin-globalization" is already insta ...

The most efficient method for personalizing the production baseUrl is using the angular2-webpack-starter

Currently, I am utilizing angular2-webpack-starter version 5.4.1. If you want to find out more about it, you can visit their GitHub page here. In order to adjust the baseUrl for the production build, I found myself repeating the same steps that were outli ...

Creating Angular components in *ngFor loop

I have set up multiple radio button groups by dynamically populating options using ngFor within a ngFor loop. categories:string[] = [category_1, ..., category_n]; options:string[] = [option_1, ..., option_n]; <fluent-radio-group *ngFor='let ca ...

Eliminate unnecessary HTML elements in Angular

I am currently using ngSwitchCase for 3 different cases, and I have noticed that I am duplicating the same code inside each case. Is there a way to eliminate this redundancy? The redundant code is located within the "app-device" component <div *ngS ...

Interactive Tab content display

I'm working on a tabs component and I need Angular to only render and initialize the active tab instead of all tabs. Is there a way to achieve this? <my-tabs> <my-tab [tabTitle]="'Tab1'"> <some-component></some-co ...

transferring data between components in a software system

I received a response from the server and now I want to pass this response to another component for display. I attempted one method but ended up with undefined results. You can check out how I tried here: how to pass one component service response to other ...

Encountering difficulties when installing dependencies in a React project

Encountering an issue while trying to add a new dependency to my React project. The error message I receive is as follows: C:\Users\abhinavverma Desktop Sodexo-Fe Matchiq-fe> npm i @cypress/instrument-cra npm ERR! code ERESOLVE npm ERR! ERES ...

Steps for calculating the average of several columns within a table using Angular 10

Currently, I have a function that successfully calculates the sum of JSON data in all columns on my tables. However, my attempt to get the average of each column is resulting in NaN or infinity. What could be the issue here? Here is my current implementat ...

Can JSPDF Autotable support line breaks with unique styles for each line?

Is it possible to create rows in jspdf autotable with two sets of information on separate lines in each cell? Currently, I am using the "\n" operator to display the information on different lines within the same cell. However, I'm looking for a ...