1

I'm attempting to pass an array of values in a hidden input using angular. Here is my code so far:

<input type="hidden" name="drug[drug_class_ids][]" value="{{selectedDrugClassIds()}}"/>

where

$scope.selectedDrugClassIds = function ()
{
    var selected_drug_class_ids = [];
    for (var x in $scope.selected_drug_classes)
    {
        selected_drug_class = $scope.selected_drug_classes[x];
        console.log(selected_drug_class);
        selected_drug_class_ids.push(selected_drug_class.id);
    }

    return selected_drug_class_ids;
};

But this is giving me the incorrect

"drug_class_ids"=>["[15,5,8]"]

Where I need

"drug_class_ids"=>["15", "5", "8"]

Any ideas how I could fix this?

1
  • Are you using PHP for your server? If you are, json_decode would work. Commented Nov 15, 2013 at 21:52

2 Answers 2

1

Got it!

This was an interesting one:

    <div ng-repeat="drug_class in selected_drug_classes">
      <input type="hidden" name="drug[drug_class_ids][]" value="{{drug_class.id}}"/>
    </div>
Sign up to request clarification or add additional context in comments.

1 Comment

Since it is all hidden, what was the purpose here ? having the fields added to the validation object ?
0

I think this is because angular stores the array as a string in the hidden field instead of the actual array. So just stringify it to json in angular and decode it in back end.

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.