I’m trying to create a reusable switcher component whose state won’t be changed immediately when clicked, but after an API call is made in parent component – if there is no error, the state will be changed, while when there is an error, the state will remain unchanged.
The problem occurs when the checkbox state is updated from the parent level, the @Input value is updated correctly, but the checkbox state remains unchanged.
Simple reproduction of the problem at the link below:
stackblitz-checkbox-issue
I tried to use ChangeDetectorRef after updating the @Input value, ngModel bindinig, but to no avail.
Any ideas what to do to update the checkbox state from the parent level to work properly?
2
Answers
Your code works if you change the value of checked under the subscribe to the API, To simulate you can declare an object or a service
And use some like, e.g.
I forked your stackblitz
NOTE: You can also force to redraw the app
You are in a complicated way…
You need to know all about angular tools…
You need to split the functionality and set in the rigth place…
So…
Your checkbox component must be reponsable for api call and then propagate the response for all components of your application…
If your component has childen the use @Input un your children.
If your component has parent use EventEmitter in simple way
If your component has parents, children, brothers compontens, use Suject/Behavior-Subject
Try to do something like this, you will gain maintaining, time, readability, good practice, ease communication, and save cost: