mercredi 3 août 2016

AngularJS - Trigger ng-change based on ng-model value

So I've ran into a problem which I have spent some time researching and to no avail, have not found a working solution.

My situation:

  • I have rows of checkboxes that fire a function with ng-change (user clicks the checkbox).
  • Each checkbox is bound to an ng-model boolean if the box is checked or not.
  • For each row, I have a 'select all' checkbox which fires a function that loops through the checkboxes in that row and updates their ng-model's bool to either true or false depending on the select all value.
  • On the DOM, the select all works fine and every checkbox bool value is changed, leading to the boxes being checked or unchecked.
  • Even if the ng-model bool is changed on the checkboxes from true to false, the ng-change function is not fired.

I have tried implementing watchers on the ng-models to try to fire each ng-change but it doesn't work.

I have tried changing ng-change to ng-click and forcing in the select all function to .trigger('click'), which sort of works but is both hacky and doesn't work correctly if one of the checkboxes are already clicked. This causes it to be the opposite if truthy value.

Is there a way to fire the ng-change function from the controller if the user doesn't directly click the checkbox?




Aucun commentaire:

Enregistrer un commentaire