mardi 22 décembre 2015

How to add object which checked in array?

I have checkboxes in the application, When I click check-box , The object which I checked, is added in array. But When I click one more time checkbox (unchecked), The object is not removed in array. How can I fix it ?

HTML Source:

<ion-list ng-repeat="option in question.SurveyOptions ">
          <li  class="item  item-checkbox checkbox-royal ">
            <label class="checkbox">
              <input type="checkbox" ng-checked="MyAnswers.indexOf(option)!=-1" ng-click="toggleCheckAnswer({OptionId:option.Id,QuestionId:question.Id})">
            </label>
            <div class="item item-text-wrap">
              {{option.OptionsName}}
            </div>
          </li>
        </ion-list>

Controller:

$scope.MyAnswers=[];
    $scope.toggleCheckAnswer=function(Answer){
      if ($scope.MyAnswers.indexOf(Answer) === -1) {
        $scope.MyAnswers.push(Answer);
      } else {
        $scope.MyAnswers.splice($scope.MyAnswers.indexOf(Answer), 1);
      }
    };

In the function Answer include only OptionId and QuestionId.

How can I find index of {OptionId:1,QuestionId:1}?




Aucun commentaire:

Enregistrer un commentaire