verifyFieldUpdate(key, value) {
// I want to do the following
// let testKey = this.key;
// key would be the name of the ion-input field in question.
let testKey = this.title;
if (value === testKey) {
console.log ('SAME');
} else {
console.log ('DIFF');
console.log('HTML Key: ', key);
console.log('HTML Value: ', value);
}
}
but of course
let testKey = this.key;
gives an error, because it does not exist, but how would I reference that variable (title / nickname / etc) from the Key passed into the function?
Should I still use ngModel even if I just want to update a single field on change?
Basically what needs to happen is that should a user want to update these details, they just change the field ie. Firstname Surname Nickname etc. but on ionBlur it then verifies if the field is the same as what is the current value, else then run an update function.
Dont know if it matters but the update function in this specific case will be a http request that updates a Wordpress User.
Okay i understand. So you should use ngModel on every input. Everytime. Going through $event.target.value is a unnecessary bad practice in angular.
Also for your context: You should not directly update after the input blur event. You should think about if a “Save” Button isn’t a better opportunity for you.
But if you really want to do it like this, you should declare 4 variables like “newTitle”, “newName”, etc and bind them via ngModel to the input. When the Page loads (OnInit Methode) you set these variables to the current user data. Then in the ionBlur you can compare this Values to the current user data. But you should not use the key thing, just compare all variables.
Let’s come back to the save Button. I prefer this and you can automatically show or hide it, depends on when the user data changed or not.
Can you perhaps elaborate a bit on why this is a better option? I understand for a blank form the first time, but in the case of just updating a specific field?
then regarding:
the 4 fields I have given was just an example of 4 fields, however in reality there can be X amount of fields.
Will I still have to declare them each individually even if there are 50 fields? This is where my reasoning for “the key thing” came to be
I understand your question as type of an user is updating his profile. A normal UX is that you have to save the changes, that’s why i’ preffering this.
If you have X Variables (probably in an object) i think you should copy the whole object to use for the two way data binding and then compare the values of the two objects.
In addition to @EinfachHans’s excellent advice here, another option for deep copying that I prefer is lodash’s clone(). If you install it as lodash-es, you won’t pull in the entire library, just the specific functions you’re using.
Additionally, you might want to consider a reactive form instead of ngModel. Reactive form controls know whether they’ve been touched or changed, and have a lot of validation infrastructure.