webcat12345 / ngx-ui-switch

Angular UI Switch component
https://webcat12345.github.io/ngx-ui-switch/demo/
MIT License
111 stars 68 forks source link

ngModelChange is being triggered on initial load #457

Open gldmtr opened 2 years ago

gldmtr commented 2 years ago

Hi. First of all - thank you for the library.

After update from version 10 to 12 I noticed that ngModelChange is triggered right after the ui-switch component appears on a page. It seems to be related to the commit https://github.com/webcat12345/ngx-ui-switch/commit/1382bb140422c5efca4df9d6e15030da3535c2e2 Could you please explain what was the fix about? Is it supposed to convert incoming non-boolean value into a boolean?

The issue example: https://stackblitz.com/edit/ngx-ui-switch-kz2sgk alert is called right after the page init.

cmckni3 commented 2 years ago

I don’t remember exactly. It might have been to set the initial value from ngModel on component init.

gldmtr commented 2 years ago

That probably makes sense to set an initial value but it causes some issues with side-effects being called even if a value haven't been changed.

zac-farrugia commented 2 years ago

Also experiencing the same problem.

zac-farrugia commented 2 years ago

I'm using this component to invoke an API based on the change event. As of v10.0.1, it is triggering the change event on initialization, resulting in the API being called.

For Example, I have a settings page where toggling the ui-switch will immediately call the API to disable functionality. If I use versions 10.0.1 or greater, then every time I open my settings page, it's toggling the change event.

dgwaldo commented 1 year ago

Any update on this? If you are calling an API on this load event, it can cause pretty wild issues. Any work arounds anyone has come up with?

cmckni3 commented 1 year ago

Any update on this? If you are calling an API on this load event, it can cause pretty wild issues. Any work arounds anyone has come up with?

Not sure. I might revert the change in the commit mentioned in the first comment.

spborhade21 commented 1 year ago

Hi @cmckni3, Are you planning to revert the changes becuase of which this intial load issue is appearing? I have used this package in my project and recently updated the version as well. Now I have to handle this inital load by adding some extra code. Let us know.

blazekv commented 10 months ago

@cmckni3 I would suggest to revert that commit. Because there is another issue with this line of code. See https://github.com/webcat12345/ngx-ui-switch/issues/493