0

I am trying to add the delete function for the option list: But always getting the error message: indexOf is undefined!

Could someone help me on that? Thanks in advance!

Here is part of my code:

Html:

<div class="question_typeList" ng-switch-default>
                    <table class="question_heading">
                        <tbody>
                            <tr ng-repeat="option in question.options">
                                <td>
                                    <input class="question_textfield" placeholder="My Option" ng-model="option.value[lang]">
                                    <button ng-click="removeOption(option)">X</button>
                                </td>
                                <td>
                                    {{option.value}}
                                </td>
                            </tr>
                        </tbody>
                        {{question.options}}
                    </table>    
                    <button ng-click="newOption(question)">Add Options</button>
                </div>

js part:

$scope.questions = [
        {
          title: $scope.newTranslatable("Title"),
          description: $scope.newTranslatable("Mr./Mrs./Ms."),
          type: "list",
          options: [
            {
              value: $scope.newTranslatable("Mr")
            }, {
              value: $scope.newTranslatable("Mrs")
            }, {
              value: $scope.newTranslatable("Ms")
            }
          ]
        }


$scope.removeOption = function(option) {
        var index = $scope.questions.options.indexOf(option);
        if(index != -1) {
          $scope.questions.options.splice(index, 1);
        }
      }
Justin
  • 2,765
  • 6
  • 24
  • 25

1 Answers1

1

$scope.questions is defined as an array. options is not a property of the array, but a property of an element of the array.

you'll have to iterate through the $scope.questions array. This raises a new problem because as far as I can tell, your questions array doesn't appear to have a «key» field to compare against.

Still, assuming you'll manage to get an unique field in there, this is how I think you could implement a function to remove a question[x].options option:

    $scope.removeOption = function(uniqueKey,option) { 
       var questionIndex = $scope.questions.yourUniqueKeyField.indexOf(uniqueKey);
       if(questionIndex != -1){
          var optionIndex = $scope.questions[questionIndex].options.indexOf(option);
          if(optionIndex != -1) {
             $scope.questions[questionIndex].options[optionIndex].splice(optionIndex,1);
          }
       }
    }