1

I currently have a form with a <select> and options as below. I am trying to pass the values into my db, but when running the app category is coming out as undefined. The form is using ng-controller="eventCtrl"

Form Select Code Below.

<div class="input-field col s5">
 <select class="browser-default">
  <option value="" disabled selected>Choose your category</option>
  <option value="Mountain" ng-model="category">Mountain</option>
  <option value="Forest" ng-model="category">Forest</option>
  <option value="Beach & Sea" ng-model="category">Beach & Sea</option>
  <option value="Fresh Water" ng-model="category">Fresh Water</option>
  <option value="Aero" ng-model="category">Aero</option>
  <option value="Desert" ng-model="category">Desert</option>
 </select>
</div>

eventCtrl Code Below.

angular.module('socialApp')
 .controller('eventCtrl', function($scope, $http) {
  $scope.createEvent = function() {
   $http.post('/events/createEvent', {
    title: $scope.title,
    category: $scope.category,
    city: $scope.city,
    state: $scope.state,
    date: $scope.date,
    time: $scope.time,
    description: $scope.description
  })
  .then(function(result) {
    console.log(result.data.status);
    console.log(result);
  })
 };
});

I am so lost with this please help!

2 Answers 2

5

The ng-model attribute should be on the select element instead of each option element as so:

<div class="input-field col s5">
 <select class="browser-default" ng-model="category">
  <option value="" disabled selected>Choose your category</option>
  <option value="Mountain">Mountain</option>
  <option value="Forest">Forest</option>
  <option value="Beach & Sea">Beach & Sea</option>
  <option value="Fresh Water">Fresh Water</option>
  <option value="Aero">Aero</option>
  <option value="Desert">Desert</option>
 </select>
</div>

I'd definitely look up the documentation on this as it's very detailed!

Sign up to request clarification or add additional context in comments.

4 Comments

Also, consider using the ngOptions directive docs.angularjs.org/api/ng/directive/ngOptions
Yes the ngOptions directive is very good to consider. Personally I use static HTML lists with angular if it is a) a long list and b) is not going to change. This is because ng-options and ng-repeat can have performance issues with very long lists!
@donghoonkim, if this or any other future answer solves your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this.
@papakia - as you can tell, I am still a noob when it comes to stack. Thanks for helping out with this issue, I really appreciate it!
0

Consider using ngOptions like this:

<select ng-model="category" 
        ng-options="obj as obj.name for obj in arrayOfObject" />

The a as b syntax lets you bind a to the model, while showing b for your dropdown label.

If in case you need to get the value on change use ng-change="getSelectedValue(category)"

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.