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);
        }
      }
6
  • Have you tried this? stackoverflow.com/questions/1744310/… Commented Mar 18, 2014 at 0:01
  • 1
    Is $scope.questions an array? Seems like there are errors in the JavaScript you posted. Commented Mar 18, 2014 at 0:11
  • @AnthonyChu questions looks like an array, am i right? Commented Mar 18, 2014 at 0:15
  • @AnthonyChu what's the error you think? Commented Mar 18, 2014 at 0:16
  • The [ at the end of $scope.questions = [ doesn't have a matching ] Commented Mar 18, 2014 at 0:18

1 Answer 1

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);
          }
       }
    }
Sign up to request clarification or add additional context in comments.

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.