TLDR: Angular’s change detection somehow doesn’t work on a phone. View updates slowly after random time intervall.
I stumbled upon the weirdest bug ever. It’s only observable when the app is installed on a phone, but not on when serving it using ionic serve. (Don’t mind the dirty code later pls.)
I have a variable called amount: number = 1;
I have two buttons, a plus and a minus one. Here is their html:
As you can see these methods are very unclean. But this is the only way I was able to make it work.
Expected behaviour:
As soon as someone clicks a button the number changes according to what he has clicked. Observed behaviour (without my dirty functions):
When someone clicks and changes the amount it takes a random time until the view updates and the number on screen changes. (The disabled class gets applied on the button instantly, therefore I know that the number gets changed instantly when the number is reduced from two to one or increased from one to two. The bug can’t / shouldn’t be in the code’s logic.)
I solved this problem forcing a resize of the screen every time the function is called. But this shouldn’t happen. What’s wrong with Angular’s change detection? Or did I do something wrong?
Sorry if I might have been unclear, englisch is not my first language.
I thought it was clear, that the buttons I was referring to, where the columns which had a click method on it
This one: <ion-col col-4 text-right (click)="changeAmount(false)">,
And this one: <ion-col col-4 (click)="changeAmount(true)">.
Those aren’t buttons. You would not have this problem if you were using buttons. They are clickable Ionic components. Ionic is built on top of Angular, so its components may or may not be inside the Angular ViewContainer, depending on what you are doing. ion-select and ion-checkbox are not checked by Angular change detection, for example.
You can add the tappable directive, to see if that helps. <ion-col tappable></ion-col>
In general, though, if you want the user to interact with a pure Ionic component, you might have to fire change detection manually.
You can definitely make divs clickable, they absolutely do not have to be buttons. I can’t reproduce your bug, this works totally fine for me both in chrome and running on my android.
That’s what I do actually. <div tappable (click)><ion-card></ion-card></div>
That way the click is on something I know Angular will recognize across platform. I almost never listen for a click inside an Ionic component, just in span or div.
Thank you for your answer! I tested on several Android devices actually. The only one where it was instant without my hacky rework was a google pixel 2.
Otherwise on like a Samsung Galaxy S7 for example, or and iPhone 8 it would show the described observed behaviour.
It might have something to do with the stuff wrapped around it.
Does the code I posted work on your pixel 2? Coincidentally that’s the phone I have and where my code ran just fine.
I’m much more concerned with your code that modifies the class of the grid cells than any of the html, since you’re not really doing that in in the angular way.
But regarding the code you posted there, there is no reason to use multiple grids. Use one grid, put multiple rows in it. Otherwise I don’t really see any issues. The big thing is…does my code work where yours doesn’t?