0

I want to do a very simple ng-model with a select and save the data to ticket.category, but I don't get any output inside <p>

The options appear on the select but the selected option doesnt show on the paragraph

What am I doing wrong?

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
<select id="category-select" name="category">
  <option ng-repeat="category in categories" 
          value="{{category.TICKET_CATEGORY_DESCRIPTION}}" 
          ng-model="ticket.category" required>
    {{category.TICKET_CATEGORY_DESCRIPTION}}
  </option>
</select>
<p>Hi {{ticket.category}}</p>
Aleksey Solovey
  • 4,153
  • 3
  • 15
  • 34
  • 2
    `ng-model` should be at the `select`, not at the `option` (so does the `require`) – Capricorn Jun 25 '18 at 09:37
  • See [Using `ng-repeat` to generate `select` options](https://docs.angularjs.org/api/ng/directive/select#using-ngrepeat-to-generate-select-options). – georgeawg Jun 25 '18 at 18:24

1 Answers1

0

You should put ng-model select element

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
<select id="category-select" name="category" ng-model="category">
  <option ng-repeat="category in categories" value="{{category.TICKET_CATEGORY_DESCRIPTION}}" required ng-selected="$index=0">
    {{category.TICKET_CATEGORY_DESCRIPTION}}
  </option>
</select>
<p>Hi {{ticket.category}}</p>
  • that worked but it inserted a blank default option in my select. How do I remove it? –  Jun 25 '18 at 09:14
  • add ng-selected="$index=0" to preset default selected option – Kasabucki Alexandr Jun 25 '18 at 09:22
  • Bad advice. See [AngularJS. When select have ng-model attribute, ng-selected not working](https://stackoverflow.com/questions/34996714/angularjs-when-select-have-ng-model-attribute-ng-selected-not-working). – georgeawg Jun 25 '18 at 18:22