A
A
Alexander Evgenievich2015-05-05 12:59:41
JavaScript
Alexander Evgenievich, 2015-05-05 12:59:41

Correct way to bind checkbox-group like array of arguments with ng-model?

Hello.
In fact, how would it be most correct to pass to $scope.query.skills = [] the values ​​of the group of checkboxes that will be formed using PHP tools.
How about ng-model="query.skills[INDEX_OF]" ?
Maybe using ng-change="setValue($element)" ?
Is there any other suitable way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
_
_ _, 2015-05-05
@banderos120

In the case of ng-model, you will need a hash. Like this:

query.skills = {
  1: true,
  2: false
};

<input type="checkbox" ng-model="query.skills[1]"/>
<input type="checkbox" ng-model="query.skills[2]"/>

false-values ​​can not be set initially, but we must remember that after the user has turned the checkbox on and off, the hash will contain the corresponding. key with false.
ngChanged is needed more in order to process changes without having to track them through $scope.$watch.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question