martinedwards
martinedwards

Reputation: 5825

AngularJS: Retain dropdown option after form submit

I'm using AngularJS to populate a dropdown, is there a way to retain which option was chosen after a form submit?

I'm also using PHP so I have the $_POST values, which I would usually write something like:

<?php if ($_POST['dropdown'] === 'this value') { ?> selected<?php } ?>

But I cannot in this case as the options are in a loop:

<option ng-repeat="type in Types" value="{{type.Name}}">{{type.Name}}</option>

Any help would be greatly appreciated!

Upvotes: 0

Views: 1027

Answers (1)

Gho5t
Gho5t

Reputation: 1060

You're going to need to make an ajax call to get the submitted value and then set the selected property in the dropdown using ng-selected: https://docs.angularjs.org/api/ng/directive/ngSelected

Angular generally is for single page applications with no postbacks, hence the hackery needed.

Upvotes: 1

Related Questions