0

I might miss some basic Angular concept because my ng-change event marks both checkboxes instead of just one.

These are my views (html file) (note the <input> tag):

<li>
    <label for="id_trade_type_0">Trade type:</label> 
    <ul id="id_trade_type">
        <li>
            <label for="id_trade_type_0">
                <input id="id_trade_type_0" name="trade_type" ng-change="filterMarkers()" ng-false-value="2" ng-model="formData.trade_type" ng-true-value="1" type="checkbox" value="1" /> Pardavimas
            </label>
        </li>
        <li>
            <label for="id_trade_type_1">
                <input id="id_trade_type_1" name="trade_type" ng-change="filterMarkers()" ng-false-value="2" ng-model="formData.trade_type" ng-true-value="1" type="checkbox" value="2" /> Nuoma
            </label>
        </li>
    </ul>
</li>
</ul>

This is the function that is in my controller:

$scope.filterMarkers = function() {
  $scope.clearMarkers();
  $scope.loadMarkers('/get_markers/?' + $.param($scope.formData));
  console.log($scope.formData)
}

When I remove ng-model="formData.trade_type" from my view, I am able to click checkboxes separately (but then of course the code does not work as it should).

What am I missing ?

3 Answers 3

2

This is because you have used same ng-model="formData.trade_type" for both the input.

use a different model to store a different value

like ex: ng-model="formData.trade_type_1" and ng-model="formData.trade_type_2"

Edit: As you have different values, If you want to use the same model you can use radio-buttons instead of select box, then only one will be selected

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the answer, I understand the problem now. The worst thing is that I must use same model and now must find a way how to send both checkboxes value as formData.
use a radio-button instead of checkbox, if you want to use the same model and select only one value
Sorry for being unclear. Plese check my controller. When I am using radio buttons in my other fields (just 1 selection), my controller's $scope.loadMarkers constructs an url of, for example, /get_markers/?property_parent=12 or /get_markers/property_type=23. But in this partucular case I want to use checkboxes and I want my controller to construct an url of /get_markers/trade_type=1&trade_type=2&trade_type=123 (that's why I need those checkboxes for and thats why I need same model). Could you please tell me what information should I search for to achieve such goal?
ok, I understood, i got a similar problem, i will post another answer for you.
0

As you said, Here is a way to store different checkbox values in an array

$scope.selectedValues = [];

$scope.select = function (item) {
   var index = $scope.selectedValues.indexOf(item.value);
   if (item.isChecked) { // adding the value if checked and not exist in array
      if (index == -1) {
         $scope.selectedValues.push(item.value);
      }
   } else {
      if (index > -1) { // removing the value if uncheked and exist in array
         $scope.selectedValues.splice(index, 1);
      }
   }
}

In template:

<input type='checkbox' ng-model='item.isChecked' ng-value='item.value' 
ng-change='select(item)'/>

The values you want will be stored in $scope.selectedValues, you can construct a query string from that values

Note: make sure your item is an object, and should have item.value property assigned to get the value

4 Comments

Thanks for the answer. Unfortunately both checkboxes are checked when I click on one of them, maybe the reason behind that is that ng-model on both checkboxes is still the same (ng-model='item.isChecked') ?..
Yes, I thought you were looping if you have multiple values, so each item will be different object. You must loop on the values to get this working
1. You must loop on the values to get this code working, OR 2. use different ng-model and use your previous code
Nope, unfortunately I am not looping, the fields are generated by Django framework's ModelMultipleChoiceFilter : trade_type = django_filters.ModelMultipleChoiceFilter(queryset=Trade.objects.all(), widget=CheckboxSelectMultiple(attrs={'ng-model': "formData.isChecked", "ng-change": "select(formData)", "ng-value" : "formData.property_type", }))
0

For both of your inputs you are using the same ng-model attribute. You can see example here: https://docs.angularjs.org/api/ng/input/input%5Bcheckbox%5D

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.